isset() && !empty()

Posted on November 24, 2014, 10:00 am by about-dev.com


When I have found the first time this condition: isset($var) && !empty($var) (that was a few years ago), it put me a little bit to think.
It didn't cross my mind to use this condition and I was wondering what was the purpose for using it.
I knew that isset checks if a variable is defined and !empty checks if a variable has a value different than 0 or "". But to check if a variable has a value, you must check first if the variable is defined. As result I consider that !empty includes isset.

So I didn't see the point of using isset && !empty in the same condition.
If you want to check if a variable is defined but you don't care about its value, you should use isset.
If you want to check if a variable has a value different than 0 and "", you should use just !empty, without isset, as you can't have an undefined variable that has a value.

After that, I have found the same condition: isset($var) && !empty($var) on various source codes written by different developers from beginners to seniors. I have asked each of them why are they using this condition, maybe I'm missing something. Each of them said that isset checks if $var is defined and !empty checks if it has a value. At first view it would appear to make sense. And it would make sense if the !empty would assume that the $var is already defined and if it would throw an error or something, but !empty returns false for a variable not defined and it doesn't even generates a warning.

Let's try to split each expression in little pieces (probably in the checking order). If any of the checks fails, the rest of the verification process won't continue, it will be stopped and it will return false.

- isset($var) checks:
1. $var is defined
2. $var is not NULL

- !empty($var) checks:
1. $var is defined
2. $var is not NULL
3. $var is not ""
4. $var is not 0
5. $var is not 0.0
6. $var is not "0"
7. $var is not FALSE
8. $var is not array()

We can see that the first checks are the same, and if you put the condition:
- isset($var) && !empty($var):
1. $var is defined
2. $var is not NULL
3. $var is defined
4. $var is not NULL
5. $var is not ""
6. $var is not 0
7. $var is not 0.0
8. $var is not "0"
9. $var is not FALSE
10. $var is not array()

you will have the first 2 conditions twice.
Isn't it redundant?

You can also find on php.net that empty($var) is the short version for: !isset($var) || $var == false.
This means that !empty($var) is the short version for: isset($var) && $var != false.
As a result isset($var) && !empty($var) would be the short version for: isset($var) && isset($var) && $var != false , wouldn't it?
How about now? Isn't it obvious?

I felt the need to look for a benchmark, to see which of these 2 possible conditions would be faster. And because I haven't found one, I tried to make myself a little code to determine if the performance would be a good reason to choose one of the 2.

I have made a repeatable loop of 10 million times, checking the same 9 variables, one for each possible situations that could be of our interest.

Here is the code snippet:

<?php
$start = microtime(true);

//$var1 = undefined
$var2 = null;
$var3 = 0;
$var4 = 0.0;
$var5 = '0';
$var6 = '';
$var7 = array();
$var8 = false;
$var9;
for ($i = 0; $i <= 10000000; ++$i) {
 for ($j = 1; $j <= 9; ++$j) {
  $varName = 'var' . $i;
  if (!empty($$varName)) {
  //if (isset($$varName) && !empty($$varName)) {
   $res = 1;
  }
 }
}
echo microtime(true) - $start;

and the results for 90 million operations are as follows:

- !empty() got an average of 28.83 seconds with a standard deviation of 0.268

- isset() && !empty() got a lower average: 28.69 seconds and a higher standard deviation: 0.297

I am not sure if these results are so conclusive.


Leave a Comment:

User
Email
Website

Blog Search

Popular Blog Categories

Newsletter

Want to be informed about latest posts? Subscribe to our newsletter