Categories
Uncategorized

PHP's empty() Function

This code snippet from http://php.net/empty illustrates a very useful function that helps eliminate some of the ambiguity we sometimes encounter with variables that have no value or boolean zero value.  The comments in the snippet explain it pretty well.

 

1
2
3
4
5
6
7
8
9
10
11
$var = 0;
 
// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}
 
// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}

[ad]

Leave a Reply