php
  1. php-reverse-string

PHP Reverse String

In PHP, we can easily reverse a given string using built-in functions. In this article, we will learn how to reverse a string in PHP using different methods.

Syntax

string strrev ( string $string )

The strrev() function takes a string as input and returns the reversed string.

Example

$input = "Hello World!";
$reversed = strrev($input);
echo $reversed;

Output

!dlroW olleH

Explanation

In the above example, we first declare a variable $input and initialize it with the string, "Hello World!". We then pass this variable to the strrev() function.

The strrev() function reverses the input string and stores the reversed string in the $reversed variable.

Finally, we print the reversed string using the echo statement.

Use

Reversing a string is a common task in programming, especially when dealing with strings in reverse order. This can be useful when working with specific data structures, such as stacks or queues.

Important Points

  • The strrev() function takes a string as input and returns the reversed string.
  • Reversing a string can be useful when working with specific data structures, such as stacks or queues.

Summary

The strrev() function in PHP can easily reverse a given string. Reversing a string is a common task in programming, and it can be useful when working with specific data structures.

Published on: