PHP empty Keyword
Example
Check if a variable is empty:
<?php
$str = "";
if(empty($str)) {
echo "The string is
empty";
}
?>
Try it Yourself »
Definition and Usage
The empty
keyword acts as a function which returns true if a variable does not exist, or if
its value is considered empty. The empty
keyword also evaluates expressions which are
not in a variable.
A value is considered empty if its value is any of the following:
- An empty string
- An empty array
- The integer 0
- The floating point number 0.0
- The string "0"
- Boolean false
- null
More Examples
Example
Use empty
on a variety of different expressions:
<?php
// A variable that does not exist
if(empty($x)) {
echo '$x does not exist<br>';
}
// An empty integer
if(empty(0)) {
echo '0 is empty<br>';
}
// An empty float
if(empty(0.0)) {
echo '0.0 is empty<br>';
}
// An empty string
if(empty("")) {
echo '"" is an empty string<br>';
}
// null
if(empty(null)) {
echo 'null is empty<br>';
}
// A value that is not empty
if(empty('A')) {
echo '"A" is empty<br>';
} else {
echo
'"A" is not empty<br>';
}
?>
Try it Yourself »
❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.