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:

  1. if ($a) – This generates a notice if $a is not set
  2. if (isset($a)) – A simple clean way to check if the variable is set (note that it is not equivalent to the one above)
  3. if (isset($a) && ($a) – The one that I have been using which is equivalent to if($a), but doesn’t generate a notice.
  4. 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:

    With NOTICE: 0.19779300689697
    With isset:  0.19768500328064 / Diff: 100.05463419811
    With both:   0.21704912185669 / Diff: 91.128222590815
    with !empty: 0.19779801368713 / Diff: 99.997468735875

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:

$myvalue = !empty($_REQUEST['myvalue']) ? $_REQUEST['myvalue'] : '';


$myvalue = isset($_REQUEST['myvalue']) ? $_REQUEST['myvalue'] : '';

7 thoughts on “PHP Performance – isset() versus empty() versus PHP Notices”

  1. 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.

    function doNotice() {
    if($a) echo "AH!";

    function doIsset() {
    if(isset($a)) echo "AH!";

    function doBoth() {
    if(isset($a) && $a) echo "AH!";

    function doEmpty() {
    if(!empty($a)) echo "AH!";

    $t = microtime(true);
    for($i = 0; $i < 1000000; $i++)
    echo 'if($a): '.(microtime(true)-$t)."\n";

    $t = microtime(true);
    for($i = 0; $i < 1000000; $i++)
    echo 'isset($a): '.(microtime(true)-$t)."\n";

    $t = microtime(true);
    for($i = 0; $i < 1000000; $i++)
    echo 'isset($a) && $a: '.(microtime(true)-$t)."\n";

    $t = microtime(true);
    for($i = 0; $i

    Which gives the following results:

    if($a): 0.63301992416382
    isset($a): 0.26364302635193
    isset($a) && $a: 0.27497410774231
    empty($a): 0.26639103889465

    Even this is slightly faster:

    isset($a); // Just to make sure it is declared.
    if($a) { }

  2. @Lucas: do you mean using empty($a) where $a doesn’t exist? This seems to work fine without a notice..

  3. 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.

  4. Been a few years so I did the same check as Frode Børli, my results were:

    if($a): 6.3749399185181 (error reporting to log) -avoid this no matter what haha
    if($a): 0.35815000534058 (no error reporting) -almost 10 times as slow as isset()
    isset($a): 0.039632081985474
    isset($a) && $a: 0.046839952468872
    doempty($a): 0.039687871932983
    !empty($a) && $a: 0.053149938583374

Leave a Reply

Your email address will not be published. Required fields are marked *