Q:

Reverse String in Php

belongs to collection: PHP Programs

0

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

For example, on reversing JAVATPOINT it will become TNIOPTAVAJ.

Logic:

  • Assign the string to a variable.
  • Calculate length of the string.
  • Declare variable to hold reverse string.
  • Run for loop.
  • Concatenate string inside for loop.
  • Display reversed string.

All Answers

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

Reverse String using strrev() function

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

Example:

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

Output:

 

Reverse String Without using strrev() function

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

Example:

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

Output:

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

total answers (1)

Swapping two numbers in Php... >>
<< Reverse number in Php...