Q:

Write a PHP program to create a new string using 3 copies of the first 2 characters of a given string. If the length of the given string is less than 2 use the whole string

0

Write a PHP program to create a new string using 3 copies of the first 2 characters of a given string. If the length of the given string is less than 2 use the whole string.

All Answers

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

<?php
function test($s1)
 { 
         $extra_Front = "";
  
         if (strlen($s1) < 2)
            {
                return $s1 . $s1 . $s1;
            }
            else
            {
                $extra_Front = substr($s1, 0, 2);
                return $extra_Front . $extra_Front . $extra_Front;
            }
 }

echo test("abc") . "\n";
echo test("Python") . "\n";
echo test("J") . "\n";

Sample Output:

ababab
PyPyPy
JJJ

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