Here’s something to get you going:
How it works
Since you’re learning, here’s a brief explanation of how it works. First, assign our string to a variable ($str
). Next, we create a temporary string to store the reversed version of the string in, $results
. Next, we iterate through $str
backwards, appending the character to $results
. Then we print out our results, $results
.
<?php
$str = "Harry";
$results = "";
for ($i = strlen($str)-1; $i >= 0; $i--) {
$results .= $str[$i];
}
print($results);
?>
There is also a built in function for this
I know your teacher probably won’t appreciate this, but there is also a built in function for reversing function, strrev
.
<?php
$str = "Harry";
print(strrev($str));
?>
3
solved Write a PHP program that reads a word and prints the word in reverse. For example, if the user provides the input “Harry”, the program prints yrraH [closed]