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

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

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

الأجوبة

<?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
هل كان المحتوى مفيد؟

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

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