PHP empty() Function
empty() The function is used to check whether a variable is empty.
empty() determines whether a variable is considered empty. A variable is considered empty if it does not exist, or its value equals FALSE. empty() does not generate a warning even if the variable does not exist.
Since version 5.5, empty() supports expressions, not just variables.
Version requirement: PHP 4, PHP 5, PHP 7
Syntax
bool empty ( mixed $var )
Parameter Description:
- $var: The variable to check.
Note: Before PHP 5.5, empty() only supported variables; anything else would cause a parse error. In other words, the following code will not work:
empty(trim($name))
As an alternative, you should use:
trim($name) == false
empty() does not generate a warning even if the variable does not exist. This means empty() is essentially equivalent to !isset($var) || $var == false.
Return Value
Returns FALSE when var exists and is a non-empty, non-zero value; otherwise returns TRUE.
The following variables are considered empty:
- "" (empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
NULLFALSE- array() (an empty array)
- $var; (a variable declared but without a value)
Example
Example
The execution result is as follows:
$ivar1 为空或为 0。
$istr1 字符串不为空或不为0。
Available PHP Functions