your image

PHP Reverse String Program - javatpoint

javapoint
Related Topic
:- PHP Java information technology

Reverse String

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.

Reverse String using strrev() function

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

 

6. MVC Model & PHP Singleton pattern | Build a CMS using OOP PHP tutorial MVC [2020]

Example:

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

Output:

Reverse String Without using strrev() function

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

Example:

  1. <?php  
  2. $string = "JAVATPOINT";  
  3. $length = strlen($string);  
  4. for ($i=($length-1) ; $i >= 0 ; $i--)   
  5. {  
  6.   echo $string[$i];  
  7. }  
  8. ?>  

Output:

Comments