I am trying to find a way to reverse a string, I\'ve seen alternatives but i wanted to to it this way thinking outside the box and not using anyone else\'s code as an altern
As others said, there's strrev()
to do this.
If you want to build it on your own (for learning?): your problem is that you're starting with your index one too high - a string of length 25 is indexed from 0 to 24, so your loop has to look like this:
for ($i = $len - 1; $i >=0;$i--)
{
echo $stringExp[$i];
}
You must get $len-1
because string starts from 0
to $len-1
<?php
// Reversed string and Number
// For Example :
$str = "hello world. This is john duvey";
$number = 123456789;
$newStr = strrev($str);
$newBum = strrev($number);
echo $newStr;
echo "<br />";
echo $newBum;
OUTPUT : first : yevud nhoj si sihT .dlrow olleh second: 987654321
for ($i = $len-1; $i >=0;$i--)
{
echo $stringExp[$i];
}
Since the index starts at 0
<?php
echo strrev("PHP TUTORS");
?>
OUTPUT OF THE ABOVE SCRIPT
SROTUT PHP
Reference Source Code
<?php
/* Reverse a string with php */
echo strrev("Hello World!");
?>
Reverse a string php