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()
<?php
$string = "HELLO WORLD";
echo strrev($string);
?>
Click to know more about strrev()
Output :
Program 2 : Without using strrev()
<?php
$string = "HELLO WORLD!";
$length = strlen($string);
for ($i=($length-1) ; $i >= 0 ; $i--)
{
echo $string[$i];
}
?>
Logic :
- Store the string to a variable
- Find the length of the string, this length is used to loop through the string
- 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
- Print each alphabet according to the position
Click to know more about strlen()
Output :
Click here If you are looking for PHP Number Reverse