PHP 8 reference · String Functions
PHP strrev() Function
Reverse a string byte by byte. Fine for ASCII, but it breaks multibyte characters such as é.
PHP 8 Since 4.0
Syntax
strrev(string $string): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
Return value
The reversed string.
Examples
<?php
echo strrev("camel"), "\n";
echo strrev("12345"), "\n";
Output
lemac 54321
<?php
$word = "racecar";
echo $word === strrev($word) ? "palindrome" : "not a palindrome", "\n";
Output
palindrome
Common mistakes
- For UTF-8 text, reverse the characters instead:
implode("", array_reverse(mb_str_split($text))).
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
Related
array_reverse()· Return a new array with the elements in reverse order. String keys are kept; numeric keys are renumbered by default.str_split()· Split a string into an array of chunks of a given length, one byte each by default. Use mb_str_split() for UTF-8.- PHP Strings · tutorial