Q:

Write a PHP program to check whether a given string starts with "F" or ends with "B"

0

Write a PHP program to check whether a given string starts with "F" or ends with "B". If the string starts with "F" return "Fizz" and return "Buzz" if it ends with "B" If the string starts with "F" and ends with "B" return "FizzBuzz". In other cases return the original string

All Answers

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

<?php
function test($s)
{
    if ((substr($s,0,1)=="F") && (substr($s,strlen($s)-1,1) =="B"))
            {
                return "FizzBuzz";
            }
            else if (substr($s,0,1) == "F")
            {
                return "Fizz";
            }
            else if (substr($s,strlen($s)-1,1) =="B")
            {
                return "Buzz";
            }
            else
            {
                return $s;
            }
}

echo test("FizzBuzz")."\n";
echo test("Fizz")."\n";
echo test("Buzz")."\n";
echo test("Founder")."\n";

Sample Output:

Fizz
Fizz
Buzz

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