Write a Program to Reverse a String using Php.

Method 1

Using Loop.

<?php 
$string = "Arvind Ahir";
$reverse_string ="";  
$string_length = strlen($string); // Used to find String word length. 
for($i=($string_length-1);$i>=0;$i--){
   $reverse_string .= $string[$i]; 
} 
echo $reverse_string;
//rihA dnivrA
?>

Method 2

Without Loop And Without Direct Function.

<?php
 $string = "Arvind Ahir";
 $string_array = str_split($string); //Split into Array
 $reverse_array = array_reverse($string_array); // Reverse Array
 $reverse_string = implode('',$reverse_array); // Recombine Array to String
 echo $reverse_string;
//rihA dnivrA
?>

Method 3

With Function.

$string = "Arvind Ahir";
$reverse_string ="";  
$reverse_string = strrev($string); //Simple Function For Reverse String
echo $reverse_string;
//rihA dnivrA

Live Demo

Enter the String in Textbox

String :

After Reverse

 

Demo WebPage

demo

(Visited 103 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

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