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

Posted on December 25th, 2007 in Linux System Administration,Programming by Brandon

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'] : '';

Versus

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

4 Responses to 'PHP Performance – isset() versus empty() versus PHP Notices'

Subscribe to comments with RSS or TrackBack to 'PHP Performance – isset() versus empty() versus PHP Notices'.


  1. on August 23rd, 2010 at 11:39 am

    Great post. I was also wondering about this.

  2. Lucas said,

    on October 29th, 2010 at 2:41 pm

    The functions have the different propouse, if u try to use empty on not existent variable, that return a NOTICE


  3. on August 2nd, 2011 at 1:59 pm

    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.

    <?php
    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++)
    doNotice();
    echo 'if($a): '.(microtime(true)-$t)."\n";

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

    $t = microtime(true);
    for($i = 0; $i < 1000000; $i++)
    doBoth();
    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) { }

  4. Janek said,

    on October 5th, 2011 at 5:29 am

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

Post a comment

Please copy the string MNWMYL to the field below: