(PHP 4, PHP 5)
empty — Determine whether a variable is empty
var
Variable to be checked
Note:
empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.
Returns FALSE
if var
has a non-empty
and non-zero value.
The following things are considered to be empty:
NULL
FALSE
Version | Description |
---|---|
5.4.0 |
Checking non-numeric offsets of strings returns |
5.0.0 |
Objects with no properties are no longer considered empty. |
Example #1 A simple empty() / isset() comparison.
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>
Example #2 empty() on String Offsets
PHP 5.4 changes how empty() behaves when passed string offsets.
<?php
$expected_array_got_string = 'somestring';
var_dump(empty($expected_array_got_string['some_key']));
var_dump(empty($expected_array_got_string[0]));
var_dump(empty($expected_array_got_string['0']));
var_dump(empty($expected_array_got_string[0.5]));
var_dump(empty($expected_array_got_string['0.5']));
var_dump(empty($expected_array_got_string['0 Mostel']));
?>
Output of the above example in PHP 5.3:
bool(false) bool(false) bool(false) bool(false) bool(false) bool(false)
Output of the above example in PHP 5.4:
bool(true) bool(false) bool(false) bool(false) bool(true) bool(true)
Note: Because this is a language construct and not a function, it cannot be called using variable functions.
Note:
When using empty() on inaccessible object properties, the __isset() overloading method will be called, if declared.