12+ PHP Programs to Reverse a String

This post contains a total of 12+ Hand-Picked PHP Program examples to Reverse a String, with Source Code. All the Programs are made using PHP Programming language.

You can use the source code of these programs for educational use with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Michael Ngwerume

Made by Michael Ngwerume. Simple PHP Program to reverse a string. ( Source )

<?php
$s1="The art of Coding";
echo "s1 = $s1 <br>";
echo "Reverse string = ".strrev($s1); //will reverse the string
?>

2. By Praveen Badiger

Made by Praveen Badiger. Reverse a string in PHP without using strrev() built-in function. ( Source )

<?php
function reverseString($str){
    $stringLen = strlen($str)-1; //Get the string length and substract 1 from length as array index starts at 0.
    $reversedString = ""; //define the reversedString.
    for($i=$stringLen;$i>=0;$i--) //loop through all string characters in reverse order.
    {
        $reversedString .= $str[$i]; //append each string character to the result string.
    }
    
    return $reversedString; // return the reversed string.
}

echo strtoupper(reverseString("lived")); //call function by passing a string. just to make it look better embedding the result in strtoupper function.
?>

3. By Dushyant singh

Made by Dushyant singh. Enter your string in line ‘ $name=”lovely flower”; ‘. ( Source )

<?php
$name="lovely flower";
$reverse=strrev($name);//it create a reverse string
echo $reverse;
?>

4. By Indrajit Saha

Made by Indrajit Saha. ( Source )

<?php
echo strrev("hi!"); // outputs "!dlrow olleH"
?>

5. By Ravi

Made by V. Enter string in $a= “Hello World”; ( Source )

<?php
    $a= "Hello World";
    echo strrev($a);
?>

6. By An0ther

Made by An0ther. PHP Program to reverse a string using UTF-8. ( Source )

<?php

class An0ther{
    public function mb_strrev($text){
        $str = iconv('UTF-8','windows-1251',$text);
        $string = strrev($str);
        $str = iconv('windows-1251', 'UTF-8', $string);
        return $str;
    }
}

$text = "ΠŸΡ€ΠΈΠ²Π΅Ρ‚, Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊ!";
$cl = new An0ther();
$str = $cl->mb_strrev($text);
echo $str;

?>

7. By Serhii Ivanenko

Made by Serhii Ivanenko. Reverse Unicode string. ( Source )

<?php
function reverseUnicodeString($str)
{
    $chars = preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY);
    $middle = count($chars) / 2;
    
    for ( $i = 0, $j = count($chars)-1; $i < $middle; $i++, $j-- ) {
        $temp = $chars[$i];
        $chars[$i] = $chars[$j];
        $chars[$j] = $temp;
    }
    
    return implode('', $chars);
}

$en = "Hello World";
echo reverseUnicodeString($en);
echo "<br>";

$ru = "ΠŸΡ€ΠΈΠ²Π΅Ρ‚ ΠΌΠΈΡ€";
echo reverseUnicodeString($ru);
?>

8. By Allison Masemola

Made by Allison Masemola. Enter string in $rev = “Reverse this string”; ( Source )

<?php

   $rev = "Reverse this string";
   $rev = strrev($rev);
   echo $rev;
  
?>

9. By Roman Andreev

Made by Roman Andreev. ( Source )

<?php
   function string_reverse($string) {
       
       for($i = 0, $j = strlen($string) - 1; $i < $j; $i++, $j--) {
           
           $temp = $string[$i];
           $string[$i] = $string[$j];
           $string[$j] = $temp;
       }
       
       return $string;
   }
   
   $string = "Hello, world!";
   $reverse_string = string_reverse($string);
   echo $reverse_string;
?>

10. By Zulfikar Ahmad

Made by Zulfikar Ahmad. ( Source )

<?php

function reverse($str){
    $final = [];
    for($i = strlen($str) - 1; $i>=0 ; $i--){
        $final[] = $str[$i];
    }
    return join($final);
}
$text = "Solo Learn";
echo reverse($text);
    
?>

11. By Raghu M

Made by Raghu M. Small PHP Program to reverse a text string. ( Source )

<?php
  
  $str = 'HELLO';
   for($i=strlen($str)-1;$i>=0;$i--){
        echo $str[$i];
    }
?>

12. By Ahmed Khattab

Made by Ahmed Khattab. ( Source )

<?php
function FirstReverse($str) {
$word= '';    
$count=strlen($str);
$i=$count-1;
for($i; $i>=0 ; $i--)
    {
     $word .= $str[$i];
    }
return $word; 
}
echo FirstReverse('ahmed');

?>

13. By RAM KUMAR K

Made by RAM KUMAR K. Reverse string in php without using any string function. ( Source )

<?php
    $string="ramkumar"; $j = 0; while(!empty($string[$j])) { $j++; } for($i = ($j-1); $i >= 0; $i--) { echo $string[$i]; }
?>

14. By RomanHomya

Made by RomanHomya. ( Source )

<?php

    $string = 'Hello, world!';
    
    $reverse = strrev($string);
    
    echo $reverse;
    
?>