PHP empty() Function

PHP 可用的函数PHP Available 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 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)
  • NULL
  • FALSE
  • array()(an empty array)
  • $var;(a variable declared, but with no value)

Example

Example

<?php $ivar1=0; $istr1='Example'; if (empty($ivar1)) { echo '$ivar1' . "Is empty or is 0." . PHP_EOL; } else { echo '$ivar1' . "Is not empty or not 0." . PHP_EOL; } if (empty($istr1)) { echo '$istr1' . "Is empty or is 0." . PHP_EOL; } else { echo '$istr1' . "The string is not empty or not 0." . PHP_EOL; } ?>

The output is as follows:

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

PHP 可用的函数PHP Available Functions

Other Extensions