PHP: Reverse a String

Problem Statement:

Write a PHP function to reverse a string.

Solution Explanation:

  • PHP has a built-in function called strrev(), which reverses a string automatically.
  • We pass the string as an argument, and strrev() returns the reversed string.

<?php
function reverseString($str) {
return strrev($str);
}

echo reverseString(“hello”); // Output: “olleh”
?>

Example Output:
Input: “hello” Output: “olleh”

Leave a Reply