PHP Exercise and solutions: Displaying reverse string – codzhub

Displaying reverse string

Sample Salution:

PHP Code:

<?php
function reverse_string($str1)
{
 $n = strlen($str1);
 if($n == 1)
   {
    return $str1;
   }
 else
   {
   $n--;
   return reverse_string(substr($str1,1, $n)) . substr($str1, 0, 1);
   }
}
print_r(reverse_string('1234')."\n");
?>

Sample Output:

4321

Leave a Reply

Your email address will not be published. Required fields are marked *