Q:

Write a PHP program to create a new string where 'if' is added to the front of a given string

0

Write a PHP program to create a new string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged.

All Answers

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

<?php
function test($s) 
{
  if (strlen($s) > 2 && substr($s,0, 2) == "if")
            {
                return $s;
            }
      return "if ".$s;
}

echo test("if else")."\n";
echo test("else")."\n";
 Sample Output:
if else
if els

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