Reverse String

A string can be reversed either using strrev() function or simple PHP code.

Logic:


Reverse String using strrev() function

A reverse string program using strrev() function is shown.

Example:


<?php  
$string = "SHORAT";  
echo "Reverse string of $string is " .strrev ( $string );  
?>
    

Output

Reverse string of SHORAT is TAROHS

Reverse String Without using strrev() function

A reverse string program without using strrev() function is shown.

Example:


<?php  
$string = "SHORAT";  
$length = strlen($string);  
for ($i=($length-1) ; $i >= 0 ; $i--)   
{  
  echo $string[$i];  
}  
?>
     

Output

TAROHS