In this PHP tutorial, you shall learn how to check if given string is empty using strlen() function, with example programs.
PHP – Check if String is Empty
To check if string is empty in PHP, get the length of the string and verify if it is zero. The length of an empty string is zero.
strlen() returns zero if the string is empty, else it returns an integer representing number of characters in the string.
The syntax of condition to check if a string is empty is
</>
Copy
strlen( $string ) === 0
Examples
1. Check if given string is empty
In this example, we will take an empty string, “”, and verify if the string is empty programmatically using the above condition.
PHP Program
</>
Copy
<?php
$string = "";
if ( strlen( $string ) === 0 ) {
echo "The string is empty.";
} else {
echo "The string is not empty.";
}
?>
Output
Conclusion
In this PHP Tutorial, we learned how to check if a string is empty or not using PHP built-in function strlen().