strrev
Reverses a string.
Syntax
strrev(string $string): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The source string. |
Return value
stringThe string written backwards.
Examples
Reversing a string:
<?php
echo strrev('hello');Output:
ollehChecking for a palindrome:
<?php
$word = 'level';
var_dump($word === strrev($word));Output:
bool(true)Notes
The function reverses bytes and corrupts non-ASCII text. For that use implode('', array_reverse(mb_str_split($text))).
See also
- mb_str_split — Splits a string into characters for any alphabet.
- array_reverse — Returns an array with its elements in reverse order.
Related recipes
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.