PHP - strrev() Function



The PHP strrev() function is used to reverse a given string. This function takes a single parameter as a string and returns a new string with the characters in reverse order.

If you pass a string that has already been reversed by this function, it will reverse it back to the original string.

Syntax

Following is the syntax of the PHP strrev() function −

strrev(string $str): string

Parameters

This function accepts a single parameter, which is described below −

  • str: The input string that needs to be reversed.

Return Value

This function returns a reversed string.

Example 1

The following program demonstrates the usage of the PHP strrev() function −

<?php
   $str = "Tutorialspoint";
   echo "The original string: $str";
   echo "\nThe reverse string: ";
   #using strrev() function
   echo strrev($str);
?>

Output

This will produce the following output −

The original string: Tutorialspoint
The reverse string: tniopslairotuT

Example 2

Following is another example of the PHP strrev() function. We use this function to reverse the given string "Hello World!"−

<?php
   $str = "Hello World!";
   echo "String before reverse: $str";
   echo "\nString after reverse: ";
   #using strrev() function
   echo strrev($str);
?>

Output

After executing the above program, the following output will be displayed −

String before reverse: Hello World!
String after reverse: !dlroW olleH

Example 3: Reversing the Reversed String

In this example, we have a given string '!dlroW olleH'. Using the PHP strrev() function will reverse it −

<?php
   $str = "!dlroW olleH";
   echo "The given reversed string: $str";
   echo "\nThe string after reverse: ";
   #using strrev() function
   echo strrev($str);
?>

Output

Following is the output of the above program −

The given reversed string: !dlroW olleH
The string after reverse: Hello World!
php_function_reference.htm
Advertisements