How to check if a string is empty in PHP
In this tutorial, we are going to learn about how to check if a string is empty or not in PHP.
Checking string is empty
To check if a string is empty or not, we can use the built-in empty()
function in PHP.
The
empty()
function returns true if a string is empty; otherwise it returns false.
Here is an example:
$myString = "";
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 an empty string in PHP.
$myString = "";
if (strlen($myString) == 0) {
echo "string is empty";
}else{
echo "string is not empty";
}