To reverse a string in PHP, you can use the strrev()
function. This function returns a string in which the characters are reversed.
For example:
$string = 'Hello';
$reversed = strrev($string);
// $reversed will be equal to 'olleH'
If you want to reverse a string without using any built-in functions, you can use a loop to iterate through the string and build a new string with the characters in the reverse order.
For example:
$string = 'Hello';
$reversed = '';
for ($i = strlen($string) - 1; $i >= 0; $i--) {
$reversed .= $string[$i];
}
// $reversed will be equal to 'olleH'
You can also use the str_split()
function to split the string into an array of characters, and then use array_reverse()
to reverse the order of the elements in the array. Finally, you can use implode()
to join the elements of the array back into a string.
For example:
$string = 'Hello';
$characters = str_split($string);
$characters = array_reverse($characters);
$reversed = implode('', $characters);
// $reversed will be equal to 'olleH'