PHP empty() and the Values It Treats as Empty
PHP empty() returns true when a variable is missing or its value converts to the Boolean false. Both the integer 0 and the string '0' count as empty. A string containing a space, such as ' ', does not.
Understand what PHP treats as empty
empty($value) is a PHP language construct. It does not warn when the variable you check is undefined. For an existing value, the result follows the Boolean conversion rules. See the PHP manual on empty().
Checking an absent key with empty($arr['key']) also gives true. A declared untyped property such as public $name; defaults to null and is also empty.
Run the example and compare the values
With PHP installed, save the code in UTF-8 as empty-example.php. In that folder, run the first command to check the syntax and the second to run the file. See the PHP basics and execution guide if this is your first PHP script.
<?php
$values = [null, false, 0, 0.0, -0.0, '', '0', [], ' ', '00', 'Lorem', [0]];
var_dump(empty($missing));
foreach ($values as $value) {
var_dump(empty($value));
}
php -l empty-example.php
php empty-example.php
var_dump() prints the results in table order. The variable $missing is deliberately undefined. The foreach loop checks each array value once.
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
| Test value | Type or state | empty() result |
|---|---|---|
$missing |
Undefined variable | true |
null |
Null value | true |
false |
Boolean false | true |
0 |
Integer zero | true |
0.0 |
Floating-point zero | true |
-0.0 |
Negative floating-point zero | true |
'' |
Empty string | true |
'0' |
String zero | true |
[] |
Empty array | true |
' ' |
One space | false |
'00' |
String containing two zeros | false |
'Lorem' |
Text string | false |
[0] |
An array with one element, zero | false |
Keep zero as a valid quantity
If zero is a valid stock count, using empty() as a filter would discard it. The PHP isset() example shows how to check for a non-null value while keeping 0. Save the next snippet as a separate PHP file and run it.
<?php
$counts = ['available' => 0, 'onHold' => null];
var_dump(empty($counts['available']));
var_dump(isset($counts['available']));
var_dump(empty($counts['total']));
var_dump(isset($counts['onHold']));
var_dump(array_key_exists('onHold', $counts));
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
Both empty() and isset() return true for available because its value is zero. The missing total counts as empty. The onHold key exists with a null value, so array_key_exists() returns true for it.
Check for text beyond whitespace
To reject a label made only of ordinary spaces or tabs while accepting the string '0', require a string, use trim() to remove characters at the ends, and compare it strictly with ''. Run this as a separate PHP file.
<?php
$inputs = [
[],
['label' => " \t "],
['label' => '0'],
['label' => ' 0 '],
['label' => 0],
['label' => 'Lorem'],
];
foreach ($inputs as $input) {
$label = $input['label'] ?? null;
$hasLabel = is_string($label) && trim($label) !== '';
var_dump($hasLabel);
}
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)
?? null supplies a fallback for an absent key. The missing and whitespace-only labels give false. The strings '0', ' 0 ', and 'Lorem' give true. The integer 0 fails the string check in this example.
Diagnose unexpected results
- Use
var_dump($value)to see the type and value. The number0and the string'0'count as empty, while'00'does not. empty()can check whether an array has elements. An array such as[0]has one element, so the array is nonempty even though that element counts as empty on its own.- The default
trim()handles a defined set of characters, including ordinary spaces and tabs. If pasted text can contain Unicode whitespace, define a separate policy for those characters.



