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 when it does not exist, or when its value equals FALSE. If the variable does not exist, empty() does not generate a warning.
Since PHP 5.5, empty() supports expressions, not just variables.
Version requirements: PHP 4, PHP 5, PHP 7
Syntax
bool empty ( mixed $var )
Parameter description:
- $var: The variable to be checked.
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))
Instead, use:
trim($name) == false
empty() does not generate a warning, even if the variable does not exist. This meansempty()it is essentially!isset($var) || $var == falseequivalent.
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 with no value)
Example
Example
The output is as follows:
$ivar1 为空或为 0。 $istr1 字符串不为空或不为0。Other Extensions
PHP Available Functions