Menu Home

empty

osbo.com | PHP | Functions | empty

Determine whether a variable is empty

Syntax

empty ( mixed $var ) : bool

Parameters

var

Variable to be checked

No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

Return

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following values are considered to be empty:

"" (an empty string)

0 (0 as an integer)

0.0 (0 as a float)

"0" (0 as a string)

NULL

FALSE

array() (an empty array)

Examples

1

<?

$var = 0;

$return = empty($var);

echo $return;

?>
1

2

<?

// $var does not exist

$return = empty($var);

echo $return;

?>
1

3

<?

echo empty("") . PHP_EOL;
echo empty(0) . PHP_EOL;
echo empty(0.0) . PHP_EOL;
echo empty("0") . PHP_EOL;
echo empty(null) . PHP_EOL;
echo empty(false) . PHP_EOL;
echo empty(array());

?>
1
1
1
1
1
1
1

4

<?

$expected_array_got_string = "abc";

var_dump(empty($expected_array_got_string[0]));
var_dump(empty($expected_array_got_string[0.0]));
var_dump(empty($expected_array_got_string["0"]));
var_dump(empty($expected_array_got_string["0.0"]));
var_dump(empty($expected_array_got_string["0 abc"]));
var_dump(empty($expected_array_got_string["abc"]));

?>
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)

PHP