Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP empty() Function

PHP empty() Function

PHP Available FunctionsAvailable PHP Functions

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:

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:

Example

Example

The execution result is as follows:


$ivar1 为空或为 0。
$istr1 字符串不为空或不为0。

PHP Available FunctionsAvailable PHP Functions