The Empty Construct
The empty
construct checks if a variable is empty. It returns true
if the variable does not exist or contains an "empty" value. The variable being checked is passed as the first parameter.
Syntax
empty(mixed $var): bool
Example
Let's check several variables for emptiness:
<?php
$var1 = '';
$var2 = 0;
$var3 = null;
var_dump(empty($var1)); // true
var_dump(empty($var2)); // true
var_dump(empty($var3)); // true
?>
Code execution result:
true
true
true
Example
Let's check non-empty variables:
<?php
$var1 = 'text';
$var2 = 1;
$var3 = ['a'];
var_dump(empty($var1)); // false
var_dump(empty($var2)); // false
var_dump(empty($var3)); // false
?>
Code execution result:
false
false
false
Example
Let's check a non-existent variable:
<?php
var_dump(empty($undefinedVar));
?>
Code execution result:
true