English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP empty() function usage and example

PHP available functions

empty() The function is used to check whether a variable is empty.

empty() determines whether a variable is considered empty. If a variable does not exist or its value is equivalent to FALSE, it is considered to be non-existent. If the variable does not exist, empty() does not generate a warning.

empty() 5.5 versions after that support expressions, not just variables.

Version requirement: PHP 4, PHP 5, PHP 7

Syntax

bool empty ( mixed $var )

Parameter description:

  • $var: the variable to be checked.

Note: in PHP 5.5 Previously, empty() only supported variables; anything else would cause a parsing 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 means that 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:

  • "" (an 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 declared variable without a value)

Online example

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

The execution result is as follows:

$ivar1 is empty or 0.
$istr1 The string is not empty or not 0.

PHP available functions