A Short Journey from empty(), to Not So Empty
I recently reviewed the documented usage for the PHP empty() function in the PHP manual at http://php.net/manual/en/function.empty.php.
Like many PHP programmers, I have learned a great deal from the online manual. I most often use the Function Reference section, which provides a comprehensive explanation of how to use functions and includes nice, practical examples.
I usually consult the downloaded manual offline for a quick review, but I prefer to check out the online version for more information because it is annotated with user comments. I think the comments in the PHP manual are what makes the online documentation really shine. I actually find that I learn quite a bit from user comments -- sometimes more than from the manual itself.
(I am happy to see that Drupal's API documents, http://api.drupal.org, are starting to gather more user contributed comments too, even if it doesn't yet compare to the PHP user manual.)
Faster, Slower, Just for Readability?
In the online reference for the PHP empty() function, I came across a user comment claiming that using empty() is faster than without it by 10-13%. This is a bold statement, and so I could not help but attempt to verify it.
This was the commenter's code (simplified).
<?php
$array[] = "";
$array[] = '';
$array[] = 0;
$array[] = "0";
$array[] = NULL;
$array[] = false;
$array[] = array();
$array[] = $var;
foreach($array as $value) {
if (!empty($value)) echo 'Not empty.';
}
foreach($array as $value) {
if ($value) echo 'Not empty.';
}
?>The variable $array was set to various values that can be evaluated as empty, i.e. null, zero, false, etc. I added loops to the foreach block to iterate over one million times. It turned out that -- to the contrary of the claim -- using the empty() function took a bit more time than not using it. The difference was slight -- 1.40 seconds vs. 1.04 seconds for a million iterations.
I realized again how much value the comments add to the manual while reading other comments.
There was a reply to the comment saying that running empty() is not faster, and it provided a test result. It was quite nice to see the data that backs up the argument. Then it further stated that "the only reason to use empty() is for code readability". I paused for a moment to ponder the validity of the argument.
Not surprisingly, there was another reply to the reply saying that empty() is not just for code readability. Interestingly, those three comments were posted several months apart from each other.
Actually, empty() doesn't just serve as cleaner code; it has a useful purpose too.
The Real Meaning of empty()
It used to be a common mistake for a novice programmer to code like this:
<?php
if ($var) ...
?>If the $var was not defined before, PHP generates a warning. Since the code will still run (because it is not an error), the programmer may not care about it. If the warning is turned off in the environment the developer may never even know about it.
It is strongly recommended practice -- regardless of your error report settings -- to check if a variable is set before evaluating it.
<?php
if (isset($var)) and $var) ...
?>PHP will pass evaluation of the second expression if the first one evaluates to false, thus avoiding the warning.
If you think it is tedious to use isset() on every conditional statement, you can combine two expressions into one by using empty().
<?php
if (!empty($var)) ...
?>This also makes the code more readable too.
It is common to see usage of empty() in Drupal such as:
<?php
if (!empty($var['foo']->bar['test'])) ...
?>This way, you do not need to check if "var", "foo", "bar" and "test" exist or not.
---
isset() checks if a variable is set. However, more precisely, it "determines if a variable is set and is not NULL".
<?php
$a = NULL;
if (isset($a)) ...
if (isset($b)) ...
?>This means that both "if" statements above will evaluate to false, for different reasons; one is set to null and the other is never defined or set.
Thus, we cannot really tell the difference between a variable set to NULL and a variable that does not exist, at least not using common PHP functions like isset(). (BTW, defined() does not do what you might think it does.)
<?php
$a = $b = 1;
$a = NULL;
unset($b);
if ($a) ... // No warning
if ($b) ... // PHP warning
?>Only PHP warning will tell you the difference.
In practical sense, setting a variable to NULL is equivalent to destroying the variable. There might be very rare occasions when you really need to tell the difference.
3 comments
Nice
Great post, I often subconsciously think about what the difference is between if($var), if(isset($var)) and if (!empty($var)) and which is appropriate to use. Thanks for the great write up comparing all the options.
Thanks
Good article. The differences between empty() and isset() need all the explanation they can get. Recently got my knickers twisted when using empty to evaluate an array item that had a possible, and valid, value of 0.
is_null
Specifically you can check if a variable or expression is NULL by using is_null().
But generally, you will want to use isset() because is known to be much faster.
Post new comment