How to check whether a string is empty in PHP
To check whether a given string is empty or not, we can use the built-in empty()
function in PHP language.
The empty()
function returns true if a string is empty; otherwise it returns false.
Here is an example:
$myString = "";
// True because $myString is empty
if (empty($myString)) {
echo "String is empty.";
}else{
echo "String is not empty.";
}
Output:
String is empty.
Similarly, we can also use the strlen()
function to check for a empty string.
$myString = "";
if (strlen($myString < 0)) {
echo "String is empty.";
}else{
echo "String is not empty.";
}