The empty() function is used to check whether a variable is empty.
empty() determines whether a variable is considered empty. When a variable does not exist, or its value is equal to FALSE, then it is considered not to exist. empty() does not generate a warning if the variable does not exist.
empty() supports expressions after version 5.5, not just variables.
Version requirements: PHP 4, PHP 5, PHP 7
bool empty (mixed $var)
Parameter description:
$var: Variable to be checked.
Note: Prior to PHP 5.5, empty() only supported variables; anything else would cause a parsing error. In other words, the following code will not take effect:
empty(trim($name))
Instead, you should use:
trim($name) == false
empty() does not generate a warning, even if the variable does not exist. This means that empty()
is essentially equivalent to !isset($var) || $var == false
.
Returns FALSE when var exists and is a non-null, non-zero value, otherwise returns TRUE.
The following variables are considered empty:
"" (empty string)
0 (0 as an integer)
0.0 (0 as a floating point number)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared but without a value)
The execution result is as follows:
$ivar1 is empty or 0. $istr1 string is not empty or 0.