PHP empty() Function
PHP empty() Syntax
empty(mixed $value): bool
PHP empty() Basic examples
var_dump(empty(0));
bool(true)
Zero is considered empty.
var_dump(empty(""));
bool(true)
An empty string is considered empty.
PHP empty() Real-world usage
$input = '';
if (empty($input)) {
echo 'No input';
}
No input
Validates required user input.
PHP empty() Edge cases
var_dump(empty('0'));
bool(true)
The string \"0\" is also considered empty.
PHP empty() Common mistakes
Using empty for strict validation
empty treats many values as false.
empty('0');
isset($v) && $v !== '';
Use explicit checks when value precision matters.
PHP empty() Frequently Asked Questions
What does empty() do in PHP?
empty() checks whether a variable is empty and returns true or false.
What values are considered empty in PHP?
Values like 0, 0.0, "", "0", null, false, and empty arrays are considered empty.
Does empty() throw a warning for undefined variables?
No, empty() does not generate warnings for undefined variables.
What is the return type of empty()?
empty() returns a boolean value.
Can empty() be used on arrays?
Yes, it returns true if the array has no elements.
Is empty() the same as isset()?
No, isset() checks if a variable is set and not null, while empty() checks for empty values.
Does empty() modify the variable?
No, it only checks the value.
Can empty() be used on object properties?
Yes, but behavior depends on property accessibility.
What is a common use case for empty()?
Validating user input or checking optional values.
Is empty() faster than strlen()?
Yes, empty() is generally faster for checking emptiness.
Does empty("0") return true?
Yes, the string "0" is considered empty.
What is the best practice when using empty()?
Use it carefully when distinguishing between "0" and truly empty values.