Introduction
The empty() function helps you determine if a variable is empty, undefined, or holds a falsy value. It is widely used in form validation, conditional statements, and error prevention.
empty() is very flexible and safe because it does not generate warnings even if the variable does not exist.
What is PHP empty()?
The empty() function in PHP is used to check whether a variable is empty. It returns a boolean value based on whether the variable contains a value or not.
- Returns true (1) → if the variable is empty
- Returns false (0) → if the variable has a value
- Does not generate warnings for undefined variables
- Works with variables, arrays, and some expressions
What is considered “empty” in PHP?
A variable is considered empty if it contains:
- “” (empty string)
- 0 (integer zero)
- “0” (string zero)
- NULL
- FALSE
- Empty array []
- Undefined variable
Syntax
Basic Syntax:
empty($variable);
Example Syntax:
if(empty($var)) {
// code
}
Examples:
Example 1: Checking Empty Variable
<?php
$employee_name = "";
if(empty($employee_name)) {
echo 'employee is empty';
}
?>
Output:
Example 2: Variable with Value
<?php
$name = "John";
if(empty($name)) {
echo "Name is empty";
} else {
echo "Name has value";
}
?>
Output:
Example 3: Undefined Variable
<?php
if(empty($age)) {
echo "Age is empty or not set";
}
?>
Note: No warning will be generated.
Example 4: With Zero Value
<?php
$number = 0;
if(empty($number)) {
echo "Number is empty";
}
?>
Output:
Example 5: Empty Array
<?php
$data = [];
if(empty($data)) {
echo "Array is empty";
}
?>
Real-Life Example
Scenario: Form Validation
In real-world applications, form validation is one of the most common use cases of empty().
<?php
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Please fill all required fields";
} else {
echo "Form submitted successfully";
}
?>
Why this is useful:
- Ensures required fields are filled
- Prevents incomplete data submission
- Improves user experience
Scenario 2: API Data Check
<?php
$response = [];
if(empty($response)) {
echo "No data received from API";
}
?>
Note: Helps handle empty responses safely.
Why it is Used
The empty() function is commonly used in PHP for the following reasons:
1. Form Validation: It ensures that required fields are not left blank by users.
2. Avoid Errors: Unlike directly accessing variables, empty() avoids warnings if a variable is not defined.
3. Simplified Checks: Instead of writing multiple conditions, empty() handles all “empty” cases in one function.
4. Cleaner Code: It reduces the need for complex conditional statements.
5. Safe Variable Checking: Useful when dealing with user input, APIs, or dynamic data.
Common Mistakes
1. Confusing empty() with isset()
- empty() → checks if value is empty
- isset() → checks if variable exists and is not NULL
2. Unexpected Behavior with “0”
$value = "0";
if(empty($value)) {
echo "Empty";
}
Output: Empty (which may surprise beginners)
3. Using empty() When Value is Required
Sometimes 0 or “0” is a valid value, but empty() treats it as empty.
4. Overusing empty()
Using empty() everywhere may hide logical issues. Use it only when appropriate.
5. Not Validating Data Type
empty() does not check type, only value presence.
Conclusion
The PHP empty() method is a powerful and convenient function for checking whether a variable contains a value. It simplifies validation logic and prevents errors when working with undefined or empty data.
However, developers should be careful when using empty() because it treats values like 0 and “0” as empty, which may not always be desired
PHP empty() method – Interview Questions
Q 1: What is empty() in PHP?
Q 2: What values are considered empty?
Q 3: What does empty() return?
Q 4: Does empty() generate warnings?
Q 5: Difference between isset() and empty()?
PHP empty() method – Objective Questions (MCQs)
Q1. empty() checks whether a variable is ______.
Q2. empty() returns ______.
Q3. Which of the following is considered empty?
Q4. empty() is faster than ______.
Q5. Can empty() be used on undeclared variables?