Most to time this question is asked in Interview, how can we print a string in reverse order with for loop


//error_reporting(NULL);
$string = trim("This is a reversed string");

//find length of string including whitespace
$len =strlen($string);

//slipt sting into an array
$stringExp = str_split($string);

for ($i = $len - 1; $i >=0;$i--)
{
echo $stringExp[$i];
}

OR

you can use


<?php
echo strrev("Hello world!");

// Result :  outputs "!dlrow olleH"
?>
Advertisements