Web Programming, Linux System Administation, and Entrepreneurship in Athens Georgia

PHP Performance – isset() versus empty() versus PHP Notices

I’m cleaning up a lot of PHP code and always program with PHP error_reporting set to E_ALL and display_errors turned on so that I make sure to catch any PHP messages that come up. Since starting on this site, I have fixed literally hundreds (maybe thousands) of PHP Notices about using uninitialized variables and non-existent array indexes.

I have been fixing problems like this where $somevar is sometimes undefined:

if ($somevar)

by changing it to:

if (isset($somevar) && $somevar)

This successfully gets rid of the NOTICEs, but adds some overhead because PHP has to perform two checks. After fixing a lot of this in this manner, I’ve noticed that the pages seem to be generated a little slower.

So, to provide some conclusive results to myself, I wrote up a quick benchmarking script – available at php_empty_benchmark.php. It goes through 1,000,000 tests using each of these methods:

if ($a) – This generates a notice if $a is not set

if (isset($a)) – A simple clean way to check if the variable is set (note that it is not equivalent to the one above)

if (isset($a) && ($a) – The one that I have been using which is equivalent to if($a), but doesn’t generate a notice.

if (!empty($a)) – This is functionally equivalent to if($a), but doesn’t generate a notice.

It measures the time to perform 1 million tests using a defined percentage of values that are set. It then computes the difference as a percentage of the time taken for the original test (the one that generates the notices). A ‘diff’ of 100 means that the execution time is the same, greater than 100 means that it is faster, and less than 100 means that it is slower. A typical test produced these results:

In summary, using the if (isset($a) && $a) syntax is about 8-10% slower than generating the PHP Notice. Using !empty() should be a drop-in replacement that doesn’t generate the notice and has virtually no performance impact. Using ifset() also has no performance impact, but is not exactly the same as ‘if($a)’ since isset() will return true if the variable is set to a false value. I included it here, because it often make the code a little more readable than the !empty($a) syntax. For example:

This test is wrong. Remember that $a will be declared after the first attempt to generate a notice. No subsequent accesses will generate a notice. This test is more correct, since the test is done inside a function so A will actually not be declared.

The issue with using !empty() or if ($somevar) is that it will evaluate to false when supplied: array(), null, 0, false, and “”

Where !empty($somevar) is a dropin replacement for if ($somevar) to avoid declaration errors.
You should always define, typecast, and test your variables appropriately to ensure your data is accurate, as opposed to relying solely on !empty()
eg: if (null === $somevar) or if (0 === $somevar)
If an undefined variable notice is generated. Fix the notice as it represents a logic error in the code and is potentially harmful to your application.
There are no excuses for lazy/bad coding practices.

Additionally until 5.5 empty could only be used with variables, making things like if (empty(trim($var))) resulted in an error.