ADVERTISEMENTS

How to reverse a string in PHP

PHP provides a variety of built-in functions for working with strings. We can reverse string with help of the strrev() function which is used to reverse positioning of string letters. It takes a string as an argument and returns the reverse of that string.

The following example will explain the process of reverse a string.

<?php

$originalString = "!zlairotutE";
$reversedString = strrev($originalString);
echo $reversedString; // Output: "Etutorialz!"

?>

This function only reverse the string and it's not support multi-byte characters like utf-8 or other, to reverse a string containing multibyte characters you can use a user-defined function or mb_strrev() which is a multibyte-safe version of strrev().

PHP (Hypertext Preprocessor) is a server-side scripting language used to create web pages and web applications. It can be embedded into HTML and is often used in combination with databases such as MySQL to create dynamic, interactive websites. You can learn php from our PHP Tutorials and PHP Examples

ADVERTISEMENTS