empty
Description
The empty of Variable Handling for PHP 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)
Links
Variable Handling
- boolval
- debug_zval_dump
- doubleval
- floatval
- get_debug_type
- get_defined_vars
- get_resource_id
- get_resource_type
- gettype
- intval
- is_array
- is_bool
- is_callable
- is_countable
- is_double
- is_float
- is_int
- is_integer
- is_iterable
- is_long
- is_null
- is_numeric
- is_object
- is_real
- is_resource
- is_scalar
- is_string
- isset
- print_r
- serialize
- settype
- strval
- unserialize
- unset
- var_dump
- var_export