Current Location: Home> Latest Articles> How to Reverse a String with strrev: How to Use strrev in PHP to Reverse Strings

How to Reverse a String with strrev: How to Use strrev in PHP to Reverse Strings

gitbox 2025-08-28

In PHP, strrev() is a very convenient built-in function that can be used to reverse strings. Using this function, we can quickly flip a string from end to start. This article will explain how to use the strrev() function in PHP to reverse strings, with examples demonstrating its usage.

1. Introduction to strrev()

PHP's strrev() function takes a string as a parameter and returns the reversed version of that string. Its syntax is very simple:

<span><span><span class="hljs-title function_ invoke__">strrev</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$string</span></span><span>): </span><span><span class="hljs-keyword">string</span></span><span>
</span></span>
  • $string: The original string that needs to be reversed.

strrev() only reverses the order of characters; it does not modify the characters themselves, and it has some limitations with Unicode or multibyte characters. Therefore, if you are working with multibyte characters (such as Chinese), you should be aware of this.

2. Basic Usage

Let's look at a simple example demonstrating how to use strrev() to reverse a string:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$str</span></span><span> = </span><span><span class="hljs-string">"Hello, World!"</span></span></span>
</span><span><span class="hljs-variable">$reversedStr</span></span><span> = </span><span><span class="hljs-title function_ invoke__">strrev</span></span><span>(</span><span><span class="hljs-variable">$str</span></span></span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$reversedStr</span></span></span>;
</span><span><span class="hljs-meta">?&gt;</span></span></span>

Output:

<span><span><span class="hljs-addition">!dlroW ,olleH</span></span></span>
</span>

In this example, the strrev() function successfully reverses the string "Hello, World!" into "!dlroW ,olleH".

3. Handling User Input

In real applications, we often need to handle user input and reverse it. Here’s an example where a user submits a string, and the program outputs the reversed version:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-variable">$_SERVER</span></span><span>[</span><span><span class="hljs-string">"REQUEST_METHOD"</span></span><span>] == </span><span><span class="hljs-string">"POST"</span></span><span>) {
    </span><span><span class="hljs-variable">$inputString</span></span><span> = </span><span><span class="hljs-variable">$_POST</span></span><span>[</span><span><span class="hljs-string">'inputString'</span></span><span>];
    </span><span><span class="hljs-variable">$reversedInput</span></span><span> = </span><span><span class="hljs-title function_ invoke__">strrev</span></span><span>(</span><span><span class="hljs-variable">$inputString</span></span></span>);
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The reversed string is: "</span></span><span>. </span><span><span class="hljs-variable">$reversedInput</span></span></span>;
}
</span><span><span class="hljs-meta">?&gt;</span></span></span>
<p><form method=</span>"post"><br>
Enter string: <input type="text" name="inputString"><br>
<input type="submit" value="Reverse"><br>
</form><br>
</span>

In this example, after a user enters a string and submits it, the program displays the reversed result.

4. Reversing Word Order with strrev()

If you want to reverse not just the characters in a string but also the order of words, you can combine strrev() with explode() and implode(). Here’s an example reversing word order:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$str</span></span><span> = </span><span><span class="hljs-string">"PHP is awesome"</span></span></span>;
</span><span><span class="hljs-variable">$words</span></span><span> = </span><span><span class="hljs-title function_ invoke__">explode</span></span><span>(</span><span><span class="hljs-string">" "</span></span>, </span><span><span class="hljs-variable">$str</span></span></span>);
</span><span><span class="hljs-variable">$reversedWords</span></span><span> = </span><span><span class="hljs-title function_ invoke__">array_reverse</span></span><span>(</span><span><span class="hljs-variable">$words</span></span></span>);
</span><span><span class="hljs-variable">$reversedStr</span></span><span> = </span><span><span class="hljs-title function_ invoke__">implode</span></span><span>(</span><span><span class="hljs-string">" "</span></span>, </span><span><span class="hljs-variable">$reversedWords</span></span></span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$reversedStr</span></span></span>;
</span><span><span class="hljs-meta">?&gt;</span></span></span>

Output:

<span><span>awesome </span><span><span class="hljs-keyword">is</span></span><span> PHP
</span></span>

In this example, we first use explode() to split the string into an array of words by spaces, then use array_reverse() to reverse the word array, and finally use implode() to join the reversed array back into a string.

5. Notes

Although strrev() is convenient, it has some limitations when handling multibyte characters. If you need to work with Unicode characters (like Chinese or other multibyte characters), you can use PHP's mb_strrev() function instead:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$unicodeStr</span></span><span> = </span><span><span class="hljs-string">"你好,世界!"</span></span></span>;
</span><span><span class="hljs-variable">$reversedUnicodeStr</span></span><span> = </span><span><span class="hljs-title function_ invoke__">mb_strrev</span></span><span>(</span><span><span class="hljs-variable">$unicodeStr</span></span>, </span><span><span class="hljs-string">'UTF-8'</span></span></span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$reversedUnicodeStr</span></span></span>;
</span><span><span class="hljs-meta">?&gt;</span></span></span>

mb_strrev() is part of the mbstring extension and can correctly reverse multibyte characters.

6. Conclusion

strrev() is a very simple and useful function in PHP for reversing strings. Whether dealing with simple single-byte characters or combining functions for more complex scenarios, it can efficiently accomplish the task. When working with multibyte characters, mb_strrev() can be used to avoid breaking characters incorrectly. Mastering these basic usages will help you easily reverse strings in PHP.