IntroductionIn this article I describe how to use PHP to reverse a string and how to determine whether a string is empty. To learn some other string functions, go to:
Reverse stringTo reverse a string you can simply use the strrev() function.
ExampleThe following example shows how to reverse a string using the strrev() function. The strrev() function takes a string argument like "PHP nrael I" and returns the entire string in reverse order like "I learn PHP".
<?php
// define string
$str = "PHP nrael I";
// reverse string
// result: "detpecca sserpxE naciremA dna draCretsaM ,asiV"
$revstr = strrev($str);
echo $revstr;
?>
Output
Checking for an empty string value
If you want to determine if a string is empty then use a combination of PHP's isset() and trim() functions.
ExampleIn the following example the isset() function verifys that the string variable exists and then uses the trim() function to trim white spaces from the edges and equate it to an empty string.<?php// define string$str = " ";// check if string is empty// result: "Empty"echo (!isset($str) || trim($str) == "") ? "String is Empty" : "String is not Empty";?>
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: