PHP empty() - Check Empty Variable
The PHP empty() function is a simple yet powerful tool used to determine whether a variable is empty. It helps developers easily check stored values, avoid errors, and write cleaner conditional statements.
Introduction
The empty() function is widely used in PHP programming to verify if a variable contains any meaningful data or if it is considered "empty." This function returns true if the variable is empty, and false otherwise. "Empty" includes variables set to "" (empty string), 0 (integer zero), 0.0 (float zero), "0" (string zero), null, false, empty arrays, or variables that are not set.
Understanding how empty() interprets values will help you prevent bugs and write effective data validations.
Prerequisites
- Basic knowledge of PHP syntax and variables.
- Working PHP environment or server (PHP 5.5+ recommended, but
empty()exists since PHP 4). - Access to a code editor and command line or browser to run PHP scripts.
Setup
No special setup is required as empty() is a built-in PHP function. Ensure you have PHP installed and configured on your system. You can check your PHP version by running:
php -v
Create a PHP file such as test_empty.php to experiment with the examples below.
How to Use PHP empty() Function
The syntax of empty() is straightforward:
empty(mixed $var): bool
It accepts one parameter, the variable to check, and returns true if the variable is empty, otherwise false.
Examples
Example 1: Checking an Empty String
<?php
$var = "";
if(empty($var)) {
echo "Variable is empty.";
} else {
echo "Variable is not empty.";
}
// Output: Variable is empty.
?>
Example 2: Checking Zero (0) Value
<?php
$var = 0;
if(empty($var)) {
echo "Variable is empty.";
} else {
echo "Variable is not empty.";
}
// Output: Variable is empty.
?>
Example 3: Checking Null
<?php
$var = null;
if(empty($var)) {
echo "Variable is empty.";
} else {
echo "Variable is not empty.";
}
// Output: Variable is empty.
?>
Example 4: Checking an Unset Variable
<?php
if(empty($unsetVar)) {
echo "Variable is empty.";
} else {
echo "Variable is not empty.";
}
// Output: Variable is empty.
?>
Notice: Using empty() on an unset variable does NOT trigger a warning.
Example 5: Checking a Non-empty Array
<?php
$array = [1, 2, 3];
if(empty($array)) {
echo "Array is empty.";
} else {
echo "Array is not empty.";
}
// Output: Array is not empty.
?>
Best Practices
- Use
empty()when you want to check if a variable has a value considered "empty" in PHP (e.g., zero or empty string). - Use
isset()if you need to check whether a variable is defined and notnull. - Avoid using
empty()on expressions or function returns — it only accepts variables as its argument before PHP 5.5. - Combine
empty()with strict type checks when necessary to avoid false positives (e.g., 0 vs. false vs. null). - Remember that in PHP 5.5+ you can use
empty()on expressions but for backward compatibility prefer variables directly.
Common Mistakes
- Expecting
empty()to returnfalsefor zero values — zero is considered empty. - Using
empty()as a function on function call results in PHP versions before 5.5, which results in a parse error. - Confusing
empty()withisset().empty()checks if the variable is empty or not, whileisset()checks if a variable is set and notnull. - Using
empty()on variables with Boolean false values, which are also considered empty. - Checking empty arrays incorrectly — an empty array returns true from
empty().
Interview Questions
Junior Level Questions
- Q1: What does the
empty()function do in PHP?
A1: It checks if a variable is empty and returns true if it contains no meaningful data. - Q2: Which values are considered empty in PHP when using
empty()?
A2: Values such as "", 0, "0", null, false, empty arrays, and unset variables. - Q3: Will
empty()show a warning if you use it on an unset variable?
A3: No, it does not generate a warning for unset variables. - Q4: Can
empty()be used on strings?
A4: Yes,empty()can check if a string is empty or zero string. - Q5: Does
empty()differentiate between 0 and false?
A5: No, both 0 and false are considered empty by the function.
Mid Level Questions
- Q1: What's the difference between
empty()andisset()?
A1:empty()checks if a variable is empty;isset()checks if it is set and not null. - Q2: Can you use
empty()on function return values in PHP 5.4?
A2: No, in PHP 5.4empty()requires a variable, not a function call or expression. - Q3: How does
empty()behave with arrays?
A3: It returns true if the array has no elements (empty array). - Q4: Why might
empty()cause logical issues when checking a numeric zero?
A4: Because zero (0 or "0") is considered empty, it may result in false negatives when zero is a valid value. - Q5: What type of values would require additional checking beyond
empty()to avoid bugs?
A5: Numeric zero, boolean false, and strings "0" may need strict type comparisons.
Senior Level Questions
- Q1: How has support for expressions inside
empty()evolved from PHP 5.4 to 5.5?
A1: PHP 5.5+ supports expressions insideempty(), while before only variables were allowed. - Q2: How do you combine
empty()with type checks to ensure precise validation?
A2: Pairempty()withis_null(),is_bool(), or strict comparisons using === to differentiate empty versus valid zero or false. - Q3: What are performance considerations when using
empty()on large datasets or in loops?
A3:empty()is efficient but avoid redundant checks in heavy loops; cache results if possible to optimize. - Q4: Can custom objects implement behavior that affects how
empty()evaluates them?
A4: Yes, implementing the__isset()or__get()magic methods can influenceempty()results on object properties. - Q5: What are edge cases where
empty()might not behave as expected in PHP?
A5: Cases include overloaded properties in objects, variables that are set to "0" string or 0 integer, and usage in different PHP versions.
FAQ
Q1: What types of variables can I pass to empty()?
empty() accepts variables of any type: strings, integers, floats, arrays, booleans, and even unset variables.
Q2: Does empty() throw an error on undefined variables?
No, unlike directly accessing an undefined variable, empty() does not generate any notice or warning for undefined variables.
Q3: How is empty() different from checking if ($var == false)?
While similar, empty() considers more values as empty, e.g., "", 0, null, false, empty arrays, and unset variables. $var == false only compares loosely against boolean false.
Q4: Can I use empty() in PHP 8+ on expressions directly?
Yes, PHP 8 fully supports passing expressions into empty(). For example, empty(trim($var)) is valid.
Q5: Why should I avoid using empty() if zero is a valid value?
Because empty(0) returns true, using empty() will treat zero values as empty, possibly leading to incorrect conditional logic.
Conclusion
The PHP empty() function is an essential utility to determine if a variable contains data or is "empty" according to PHP's loosely defined rules. It simplifies checks but requires awareness of what PHP considers empty to avoid logical bugs, especially with zeros, false, and null values. By mastering empty(), alongside isset() and strict type comparisons, you can write robust PHP code that handles variable validation accurately.