empty
Checks that a variable does not exist or holds an "empty" value.
Syntax
empty(mixed $var): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $var | mixed | The variable to check. |
Return value
bool`true` for a missing variable, `null`, `false`, `0`, `'0'`, an empty string and an empty array.
Examples
Empty values:
<?php
var_dump(empty(''));
var_dump(empty([]));
var_dump(empty('text'));Output:
bool(true)
bool(true)
bool(false)A trap: the string "0" counts as empty too:
<?php
$quantity = '0';
var_dump(empty($quantity));
var_dump($quantity === '');Output:
bool(true)
bool(false)Notes
If zero is a valid value, check more precisely: $value === '' or $value === null.
See also
Related recipes
In the tutorial
- Variables — How to declare variables in PHP, name them and interpolate them into strings.