2

I want to get the text and print every words backwards.

Example: Hello World

Output: World hello

This is my codes so far. It is different, I get output backwards but per string.

$string = "hello world";  

$length = strlen($string);  
for ($i=($length-1) ; $i >= 0 ; $i--)   
{  
  echo $string[$i];  
}  

Output of the above code is:

dlrow olleh
desertnaut
  • 57,590
  • 26
  • 140
  • 166
angel1108
  • 333
  • 4
  • 22

1 Answers1

7

Another way to do it with array_reverse(),

<?php
$str = 'Hello World';
$strArray = explode(" ", $str);       
$strArray = array_reverse($strArray);       
$str = implode($strArray, " ");
echo $str;
?>

DEMO: https://3v4l.org/ZfEqQ

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103