In PHP, empty() checks whether a variable is empty. It returns true when the variable does not exist or holds a falsy value, and false otherwise.
empty() is a language construct, so it never raises a warning for an undefined variable or a missing array key. We still write it with parentheses like a function. The values it counts as empty are "", "0", 0, 0.0, null, false and an empty array.
Syntax
Call empty() with the values you need. Start with this line.
PHP
$isEmpty = empty($var);
Call
What it does
empty($name)
Returns true if $name is missing, "", "0", 0, 0.0, null, false or [].
empty($user['email'])
Returns true if the key is missing or its value is falsy, with no warning.
!empty($list)
Returns true when $list exists and has something in it.
empty($x) is the same as !isset($x) || !$x, but shorter. Because it counts 0 and "0" as empty, it can give surprises when 0 is a valid value, such as a quantity or a score.
Library declarations
PHP
// empty() is a language construct, not a regular function
empty(mixed $var): bool
Parameters
Parameter
Description
var
Required. The variable or expression to check. Missing variables and missing array keys are allowed, and do not cause a warning.
Return Value
Return Value
Description
bool
true if the variable does not exist or is falsy ("", "0", 0, 0.0, null, false, []). false if it holds any other value.
PHP Examples
Example 1 — Checking Simple Variables
Test four variables that hold different values and print each result.
$missing was never created, but empty() simply returns true. Other functions would warn about an undefined variable.
The key 'email' does not exist in $user, so empty() returns true without any warning.
The key 'name' holds "Asha", so empty() returns false.
An empty array means a cart with no items, so the ternary prints "Cart is empty".
Frequently Asked Questions
empty() returns true when a variable is not set or holds a falsy value ("", "0", 0, 0.0, null, false or an empty array). It returns false for everything else.
isset() returns true when a variable exists and is not null. empty() returns true when a variable is missing or falsy. They differ for values like 0, "0", "" and false: a variable holding 0 is set (isset() is true) but also empty (empty() is true). A variable holding null is not set (isset() is false) and is empty. Use isset() to ask "does it exist and have a value?" and empty() to ask "is it missing or falsy?".
No. It is a language construct, like isset() and unset(). You write it with parentheses, but it cannot be used as a callback such as array_filter($list, 'empty').
No. empty() is built to be safe. It returns true for a variable or array key that does not exist and does not raise a warning or notice.
PHP treats the string "0" as falsy when it converts it to a boolean. This catches many beginners out, for example when 0 is a valid quantity. If 0 is allowed, use isset($x) && $x !== "" instead.