Write a PHP program to check if a given string begins with 'abc' or 'xyz'. If the string begins with 'abc' or 'xyz' return 'abc' or 'xyz' otherwise

  • برمجة بي اتش بي

Write a PHP program to check if a given string begins with 'abc' or 'xyz'. If the string begins with 'abc' or 'xyz' return 'abc' or 'xyz' otherwise return the empty string

الأجوبة

<?php
function test($s1)
{ 
      if (substr($s1,0,3) == "abc")
            {
                return "abc";
            }
            else if (substr($s1,0,3) == "xyz")
            {
                return "xyz";
            }
            else
            {
                return "";
            }
    }

echo test("abc")."\n";
echo test("abcdef")."\n";
echo test("C")."\n";
echo test("xyz")."\n";
echo test("xyzsder")."\n";

Sample Output:

abc
abc

xyz
xyz
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...