Strings7 min readExamples run on PHP 8.5.10

strrev()

Learn how PHP 8.4 strrev() reverses a string, what it returns, how it behaves with empty input and UTF-8 text, and which mistakes to avoid.

Signature

strrev(string $string): string

Purpose

strrev() takes a string and returns a new string with the same bytes in the opposite order. The first byte becomes the last, the second becomes the second to last, and so on. It is a small helper, but it shows up in palindrome checks, simple string transformations, and quick exercises that practise string handling.

The function does not modify its argument. PHP strings are passed by value, so the original variable keeps its content unless you assign the result back to it.

Signature

Parameters and defaults

$string is the only parameter and it is required. There are no optional parameters and no default values. The function reverses whatever bytes the string contains, including spaces, punctuation, digits and newline characters.

In the default (non-strict) typing mode, a scalar such as an integer is converted to a string before reversal, so strrev(12345) behaves like strrev("12345"). If a file declares strict_types=1, passing an integer is rejected with a TypeError. Passing an array also raises a TypeError, because an array cannot be converted to a string parameter.

Return value and failure cases

The function always returns a string. It has no false return and does not emit warnings for ordinary string input. An empty string simply comes back as an empty string, and a one-byte string comes back unchanged. The only failures you are likely to meet are type errors caused by passing a value that cannot be treated as a string.

Example 1: basic reversal

This example reverses a sentence, an empty string and a numeric string. The vertical bar after the empty result makes the empty output visible.

Example
<?php
echo strrev("Hello world!"), "\n";
echo strrev(""), "|\n";
echo strrev("12345"), "\n";
Output
!dlrow olleH
|
54321

The result is a new string each time. Spaces and punctuation are moved like any other byte.

Example 2: a simple palindrome check

A classic use is comparing a string with its reverse. To make the check useful for phrases, remove everything except ASCII letters and digits first, then lowercase the remainder.

Example
<?php
function isSimplePalindrome(string $text): bool
{
    $clean = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $text));
    return $clean === strrev($clean);
}

echo json_encode([
    isSimplePalindrome("A man, a plan, a canal: Panama"),
    isSimplePalindrome("PHP 8.4"),
]), "\n";
Output
[true,false]

The strict comparison === is appropriate because both sides are strings. This version is intentionally limited to ASCII; it is not a general Unicode palindrome test.

Example 3: what happens with multibyte characters

strrev() works on bytes, not on characters. In UTF-8, the letter é is stored as two bytes. Reversing those bytes produces a sequence that is no longer valid UTF-8. The example uses escape sequences and bin2hex() so you can inspect the bytes safely instead of printing broken text.

Example
<?php
$text = "\xC3\xA9";
echo strlen($text), "\n";
echo bin2hex($text), "\n";
echo bin2hex(strrev($text)), "\n";
Output
2
c3a9
a9c3

The two bytes swap places, which is exactly what a byte-oriented reversal should do, but it is almost never what you want for human-readable non-ASCII text.

Edge cases

  • Empty string: returns an empty string.
  • Single byte: returns the same single byte.
  • Whitespace and newlines: these are ordinary bytes, so a trailing newline becomes a leading newline after reversal.
  • Numeric input: in non-strict mode a number is converted to a string, and the result is always a string, not a number. Reversing 120 gives the string "021", and the leading zero is preserved because it is still text.
  • Binary data: reversal is exact at the byte level, so reversing twice restores the original value.

Common mistakes

  1. Using it on UTF-8 text. Accented letters, non-Latin scripts and emoji consist of several bytes, and reversing them corrupts the text. For code points, a common approach is implode('', array_reverse(mb_str_split($text))), which needs the mbstring extension. Even that does not handle combining marks or grapheme clusters as users perceive them.
  2. Expecting the original variable to change. Writing strrev($name); on its own line discards the result. Assign it: $name = strrev($name);.
  3. Forgetting case and punctuation in palindrome checks. Comparing a raw phrase with its reverse fails when capitalisation, spaces or punctuation differ, so normalise first.
  4. Treating the result as a number. Reversing digits yields a string. Cast it explicitly only when you really need an integer, and remember that leading zeros disappear on conversion.
  5. Passing arrays. To reverse the order of array elements, use an array function instead.
  • array_reverse() reverses the order of array elements.
  • str_split() splits a string into byte chunks.
  • mb_str_split() splits a string into characters for a given encoding.
  • implode() joins array elements back into a string.
  • strlen() reports the byte length of a string, which is the unit strrev() works in.

Recap

strrev(string $string): string returns the bytes of its argument in reverse order. It has one required parameter, never returns false, and leaves the input untouched. It is dependable for ASCII and binary-style data, but it is not character-aware, so use a multibyte-aware approach for general Unicode text.

Tested examples

Basic reversal, empty string and numeric string
<?php
echo strrev("Hello world!"), "\n";
echo strrev(""), "|\n";
echo strrev("12345"), "\n";
Output
!dlrow olleH
|
54321
A simple ASCII palindrome check
<?php
function isSimplePalindrome(string $text): bool
{
    $clean = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $text));
    return $clean === strrev($clean);
}

echo json_encode([
    isSimplePalindrome("A man, a plan, a canal: Panama"),
    isSimplePalindrome("PHP 8.4"),
]), "\n";
Output
[true,false]
Byte-level reversal of a multibyte character
<?php
$text = "\xC3\xA9";
echo strlen($text), "\n";
echo bin2hex($text), "\n";
echo bin2hex(strrev($text)), "\n";
Output
2
c3a9
a9c3