strrev

Returns a string with its bytes in reverse order.

Signature
strrev(string $string): string
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$stringstringThe string to be reversed.

Return value

stringThe reversed string.

strrev() returns a new string with the bytes of the input in the opposite order. The original is untouched — PHP strings are values, not buffers.

When to reach for it

The honest answer is: rarely, and almost always on ASCII data. Palindrome checks, reversing a digit sequence for a checksum algorithm, generating simple obfuscated tokens, and building test fixtures are its realistic uses.

A more common trick is searching a string from the right by reversing it, but strrpos() and strripos() already do that without allocating a copy — prefer them.

What trips people up

It reverses bytes, not characters. Any UTF-8 sequence longer than one byte is torn apart, producing invalid output: strrev('héllo') is not 'olléh' but a string with a broken two-byte sequence in the middle. To reverse text safely, split into characters first:

implode('', array_reverse(preg_split('//u', $s, -1, PREG_SPLIT_NO_EMPTY)))

Even that is not enough for combining marks, flags or emoji made of several code points; those need grapheme-aware splitting.

Because a reversed multibyte string is invalid UTF-8, storing or echoing it can trigger encoding errors far away from the call that caused them.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Check whether a normalised phrase is a palindrome.
  • Reverse a digit string for a checksum routine such as Luhn.
  • Reverse each word in a sentence while keeping the word order for a puzzle or test fixture.

Frequently asked questions

Does strrev work with UTF-8?

No. It reverses individual bytes, which splits multibyte characters and produces invalid UTF-8. Split with preg_split('//u', ...) and array_reverse() instead.

How do I reverse a multibyte string in PHP?

Use implode('', array_reverse(preg_split('//u', $s, -1, PREG_SPLIT_NO_EMPTY))), or mb_str_split() if the mbstring extension is available.

Does strrev modify the original string?

No. It returns a new string and leaves the argument unchanged, like every other PHP string function.

More examples

Palindrome check

Stripping punctuation and lowercasing gives amanaplanacanalpanama, which equals its own reverse, so the check is true. The normalisation step is what makes the comparison meaningful — the raw phrase with its commas and colon is not a byte-for-byte palindrome.

Example · php
<?php
$phrase = 'A man, a plan, a canal: Panama';

$clean = strtolower(preg_replace('/[^A-Za-z0-9]/', '', $phrase));
echo $clean, PHP_EOL;

var_dump($clean === strrev($clean)); // bool(true)
var_dump('hello' === strrev('hello')); // bool(false)

Reversing safely for multibyte text

Splitting on the //u pattern yields whole characters, so array_reverse produces the correct eviän instead of the broken bytes strrev would give. The second line prints "php sekam sgnirts ysae" — safe here only because every word is ASCII.

Example · php
<?php
$s = 'näive';

$chars = preg_split('//u', $s, -1, PREG_SPLIT_NO_EMPTY);
echo implode('', array_reverse($chars)), PHP_EOL; // eviän

// reverse each word, keep word order
$sentence = 'php makes strings easy';
echo implode(' ', array_map('strrev', explode(' ', $sentence))), PHP_EOL;

Discussion

  • Be the first to comment on this lesson.