Reverse String – Qawall

reverse string

Reverse String – Qawall

Write a program to reverse string in php

We can reverse string in php using the php string function strrev() and without using the string function. Let’s check one by one.

Example : string = HELLO WORLD , reverse = DLROW OLLEH

Program 1 : using strrev()

     $string = "HELLO WORLD";  
     echo strrev($string);  

Click to know more about strrev()

Output :

Program 2 : Without using strrev()

   $string = "HELLO WORLD!";  
   $length = strlen($string);  
   for ($i=($length-1) ; $i >= 0 ; $i--)   
       echo $string[$i];  

Logic :

  1. Store the string to a variable
  2. Find the length of the string, this length is used to loop through the string
  3. Start a for loop from $length-1 , the -1 is because php indexing/position is starts from 0 also the loop will be decreased until 0
  4. Print each alphabet according to the position

Click to know more about strlen()

Output :

Click here If you are looking for PHP Number Reverse

Leave a Reply

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

Back To Top