Syntax
The syntax for the empty() function in PHP is :
empty(mixed $var): bool
It takes a single argument `$var`, which is the variable you want to check. The function returns a boolean value - either true or false.
For example :
<?php
$x = '';
if (empty($x)) {
echo 'The variable $x is empty.';
} else {
echo 'The variable $x is not empty.';
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output
The variable $x is empty.
In this case, the empty() function will return true because `$x` is an empty string. The script will output: "The variable $x is empty."
You can also use empty() directly in an if statement's condition:
<?php
$y = 0;
if (empty($y)) {
echo 'The variable $y is considered empty.';
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output
The variable $y is considered empty.
Since 0 is considered empty by the empty() function, this script will output: "The variable $y is considered empty."
Note: Remember that empty() doesn't generate any warning or error if the variable doesn't exist. This makes it a useful tool for checking variables without causing unintended side effects.
Parameter Values
The empty() function accepts a single parameter `$var`, which can be any expression that evaluates to a variable. This includes simple variables, arrays, object properties, or function results.
Let’s see some examples of valid arguments for empty():
1. Simple variable:
$x = '';
empty($x);
2. Array element:
$arr = ['a', 'b', 'c'];
empty($arr[1]);
3. Object property:
$obj = new stdClass();
$obj->prop = 0;
empty($obj->prop);
4. Function result:
function returnEmptyString() {
return '';
}
empty(returnEmptyString());
The empty() function will evaluate the given argument & determine if it is empty according to the following rules:
- "" (an empty string)
- 0 (integer)
- 0.0 (float)
- "0" (string)
- NULL
- FALSE
- array() (an empty array)
Any variable that fits one of these conditions will be considered empty.
It's important to note that empty() only checks the value of the variable itself. It does not recursively check nested arrays or object properties. For example:
$arr = [[]];
empty($arr); // Returns false, as $arr is an array with one element
empty($arr[0]); // Returns true, as $arr[0] is an empty array
Return Value
The empty() function returns a boolean value indicating whether the provided argument is considered empty or not.
It will return true if the variable is:
- An empty string ""
- An integer 0
- A float 0.0
- A string "0"
- NULL
- FALSE
- An empty array array()
In all other cases, empty() will return false.
These are a few examples showing the return values of empty():
<?php
var_dump(empty(""));
var_dump(empty(0));
var_dump(empty(0.0));
var_dump(empty("0"));
var_dump(empty(NULL));
var_dump(empty(FALSE));
var_dump(empty(array()));
var_dump(empty("abc"));
var_dump(empty(1));
var_dump(empty(1.5));
var_dump(empty(TRUE));
var_dump(empty(array(1)));
?>

You can also try this code with Online PHP Compiler
Run Code
Output
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
These return values allow you to easily use the empty() function in conditional statements to control the flow of your script based on whether a variable is considered empty or not.
For example:
$x = '';
if (empty($x)) {
echo 'The variable $x is empty.';
} else {
echo 'The variable $x is not empty.';
}

You can also try this code with Online PHP Compiler
Run Code
This script will output:
"The variable $x is empty."
Technical Details
Generally, the empty() function is a language construct rather than a regular function. This means it has some special properties & behaves differently than user-defined functions.
When you call empty(), PHP evaluates the argument expression & checks the resulting value against a set of conditions. The exact implementation may vary between PHP versions, but the general behavior remains consistent.
In PHP 8.0 & later, empty() follows these rules to determine if a value is considered empty:
1. If the value is an empty array, it returns true.
2. If the value is a string & its length is 0, it returns true.
3. If the value is NULL, it returns true.
4. If the value is a numeric string "0", it returns true.
5. If the value is a number 0 (integer or float), it returns true.
6. If the value is FALSE, it returns true.
7. If the value is a SimpleXML object created from empty tags, it returns true.
8. In all other cases, it returns false.
One important thing to note is that empty() doesn't generate any warnings or errors if the variable doesn't exist. This is different from isset(), which only returns true if a variable is set & not NULL.
Let’s see an example explaining the difference:
$x = null;
var_dump(isset($x));
var_dump(empty($x)); // bool(true)
unset($y);
var_dump(isset($y)); // bool(false)
var_dump(empty($y)); // bool(true), no warning generated

You can also try this code with Online Javascript Compiler
Run Code
Output
bool(false)
bool(true)
bool(false)
bool(true)
While empty() is generally reliable & efficient, there are a few points to keep in mind:
1. empty() only checks the value of the variable itself, not nested elements.
2. The behavior of empty() can be affected by magic methods like __isset() or __empty() in objects.
3. In older PHP versions (before 5.5), empty() would also return true for the string "false".
Note: Despite these minor disadvantages, empty() remains a powerful and commonly used tool in PHP for checking whether variables are empty or false.
Frequently Asked Questions
What is the difference between empty() and isset()?
While both functions check variables, isset() only verifies if a variable exists & is not NULL, whereas empty() also considers falsey values like 0, "", or false as empty.
Can empty() be used on object properties?
Yes, empty() can check object properties. However, the behavior may be affected by magic methods like __isset() or __empty() if defined in the object's class.
Does empty() generate a warning if the variable doesn't exist?
No, unlike isset(), empty() does not generate any warning or error if the variable being checked is not defined. It simply returns true in such cases.
Conclusion
In this article, we've explained the empty() function in PHP, which is very useful for checking if variables are empty or hold false values. We discussed its definition, usage, syntax, parameter values, and return values. We also understood some technical details.
You can also check out our other blogs on Code360.