Reverse String in PHP

- 1 minute read

PHP’s built-in strrev function will reverse a given string:

$str = 'example';

$reverse = strrev($str);

echo $reverse; // Prints "elpmaxe".

You can also use strrev with numbers, but you may need to convert your numbers back to integers or floats, because strrev will make them strings:

$num = 12345;

$reverse = strrev($num); // $reverse is now a string of value "54321".

$int = intval($num); // Conversts $reverse back to an integer.

// If $num were a float, then floatval() should be used instead.

PHP offers a few other similar built-in functions for reversing other data types.

For example, array_reverse reverses an array:

$arr = ['cat', 'dog', 'snake'];

$reverse = array_reverse($arr);

// $reverse is now an array of value ['snake', 'dog', 'cat'].

Link to this section Conclusion

I hope you enjoyed this simple little tutorial.

Happy PHP data-reversing!