empty($var) returns true if the variable does not exist or its value is falsy: null, false, 0, 0.0, "", "0" or an empty array []. For any other value it returns false. Like isset(), it never warns about undefined variables or array keys.
empty($x) is the same as !isset($x) || !$x. In other words: missing, or converts to false.
Which values are empty
Pick a value in the table to see empty() next to isset() and the (bool) cast. Every cell was computed by PHP 8.3.
| $x | empty | isset | bool |
|---|---|---|---|
| null | |||
| false | |||
| 0 | |||
| 0.0 | |||
| "" | |||
| "0" | |||
| [] | |||
| " " | |||
| "0.0" | |||
| "a" | |||
| "null" | |||
| [0] | |||
| 1 | |||
| -1 |
The surprises are at both ends: "0" is empty, while " " (a space), "0.0", "null" and [0] are not.
"0" is empty: the classic bug
Because "0" is falsy, empty() treats a quantity of 0, a score of 0 or a PIN of "0" as "nothing entered". This bites in forms, where every value arrives as a string.
For text input, the reliable check for "nothing entered" is trim($value) === "". Keep empty() for values where 0 really means nothing.
Check if a string is empty
To test for an empty string exactly, compare with "". To also treat whitespace as empty, trim first. Since PHP 5.5 empty() accepts expressions, so empty(trim($s)) works too (but has the same "0" problem).
trim() does not accept null without a deprecation notice in PHP 8.1 and later, which is why the block writes trim($s ?? "").
Check if an array is empty
For arrays, empty($arr), $arr === [] and count($arr) === 0 all give the same answer. empty() has the advantage of working on a variable or key that may not exist.
array_filter() without a callback removes every falsy value, so it is a quick way to ask "does this array hold anything real".
Validate a required form field
!empty() is a common way to require a field, as long as 0 is not a valid answer. Run this block, leave the fields blank and press Send, then fill them in and send again: the script runs once more with $_POST filled in.
empty($_POST["terms"]) covers both cases for the checkbox: not sent at all, or sent with an empty value. The forms page goes further with per-field error messages.
empty vs isset
Both are safe on undefined variables and keys. They answer different questions:
isset($x): does it exist and is it notnull? Use it for optional keys, checkboxes and "was this sent at all".!empty($x): does it exist and is it truthy? Use it for "has something real", when0and"0"do not count.$x === ""or$x === []: is it exactly empty? Use it when0is a valid value.
The isset page covers the other half, including isset() with several variables at once and the difference from array_key_exists().
Frequently Asked Questions
What values does empty() consider empty in PHP?
empty($x) is true when $x does not exist or is null, false, 0, 0.0, "", "0" or []. Everything else is not empty, including " " (a space), "0.0", "false" and [0].
Why is "0" empty in PHP?
empty() is the same as !$x without the warning for undefined variables, and the string "0" converts to false. That makes empty() wrong for fields where 0 is a valid answer; check $x === '' or trim($x) === '' instead.
How do I check if an array is empty in PHP?
empty($arr) and $arr === [] both work, and so does count($arr) === 0. empty() also returns true when the variable is not set, while count() on an undefined variable would warn and then throw a TypeError for null.
When should I use empty() instead of isset()?
Use isset() when you only need to know that a value exists (a checkbox was sent, a config key is present). Use !empty() when the value must also be non-blank, for example a required text field. Be careful with fields where 0 is valid, since empty('0') is true.