Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now