How to check for empty string in PHP ?
Last Updated :
23 Jul, 2025
In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.
here we have some common approaches
Using empty() function
The function is used to check whether the string is empty or not. It will return true if the string is empty.
Syntax:
bool empty(string)
Parameter: Variable to check whether it is empty or not.
Return Value: If string is empty, it returns true and false otherwise.
Example 1: PHP program to check whether the string is empty or not.
PHP
<?php
// Consider a string which is empty
$s = "";
// Return a message if string is empty
if(empty($s)) {
echo "Empty string";
}
else {
echo "Not empty";
}
?>
Using the strlen() function
Using the strlen() function, you can check if a string is empty by evaluating its length. If `strlen($string) === 0`, the string is empty. This approach ensures precise length measurement, distinguishing between empty strings and other empty values like `NULL` or `0`.
Example:
PHP
<?php
// Consider a string which is empty
$s = "";
// Return a message if string is empty
if (strlen($s) === 0) {
echo "Empty string";
} else {
echo "Not empty";
}
?>
Using trim() and ==
The trim() and == approach removes whitespace from both ends of a string using trim(). Then, it compares the result with an empty string using ==, which checks if the trimmed string is empty.
Example
PHP
<?php
$str =" ";
if (trim($str) =="") {
echo "String is empty" . PHP_EOL;
}else{
echo "String is not empty" . PHP_EOL;
}
?>
Using === operator
The === operator in PHP checks both the value and the type of the variable. This approach directly compares the string with an empty string, ensuring that only strings that are truly empty (with no characters) are considered empty.
Example: This method is straightforward and ensures that only truly empty strings (with no characters) are detected.
PHP
<?php
$string = "";
if ($string === "") {
echo "The string is empty";
} else {
echo "The string is not empty";
}
?>
OutputThe string is empty
Using the mb_strlen() Function
Using the mb_strlen() function in PHP, you can check for an empty string, including multibyte characters. It returns the string length, and if the result is 0, the string is empty. This is useful for handling non-ASCII text accurately.
Example
PHP
<?php
$str = " ";
if (mb_strlen(trim($str)) === 0) {
echo "String is empty" . PHP_EOL;
} else {
echo "String is not empty" . PHP_EOL;
}
?>
Using isset() Function
The isset() function checks if a variable is set and is not NULL. Combining isset() with a check for an empty string ensures that the variable exists and is not empty.
Example:
PHP
<?php
$string = "";
if (isset($string) && $string === "") {
echo "The string is empty.";
} else {
echo "The string is not empty.";
}
?>
OutputThe string is empty.
Using strlen() and trim() Function
Another approach to check if a string is empty in PHP involves combining the strlen() and trim() functions. This method ensures that even strings that only contain whitespace are considered empty.
Example: This example demonstrates how to use the strlen() and trim() functions to check if a string is empty.
PHP
$string = " ";
if (strlen(trim($string)) === 0) {
echo "The string is empty.";
} else {
echo "The string is not empty.";
}
?>
OutputThe string is empty.
Using strlen() and empty() Function
This approach combines the strlen() function to check the length of the string and the empty() function to ensure that the variable is not only empty but also set. This method provides a thorough check for empty strings.
Example:
PHP
<?php
$string = " ";
// Return a message if the string is empty after trimming and using empty()
if (empty($string) && strlen(trim($string)) === 0) {
echo "The string is empty.";
} else {
echo "The string is not empty.";
}
?>
OutputThe string is not empty.