PHP isset() vs empty() vs is_null()

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

isset()

From PHP manual – isset():

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty()
From PHP Manual – empty():

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

is_null()
From PHP Manual – is_null():

is_null — Finds whether a variable is NULL

In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.

The table below is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false).
Value of variable ($var) isset($var) empty($var) is_null($var)
“” (an empty string) bool(true) bool(true)
” ” (space) bool(true)
FALSE bool(true) bool(true)
TRUE bool(true)
array() (an empty array) bool(true) bool(true)
NULL bool(true) bool(true)
“0” (0 as a string) bool(true) bool(true)
0 (0 as an integer) bool(true) bool(true)
0.0 (0 as a float) bool(true) bool(true)
var $var; (a variable declared, but without a value) bool(true) bool(true)
NULL byte (“\ 0″) bool(true)
I have tested the above values in following environments:

  • Wampserver 2.1, Apache 2.2.21, PHP 5.3.8
  • Wampserver 2.1, Apache 2.2.17, PHP 5.3.5
  • Wampserver 2.1, Apache 2.2.17, PHP 5.2.11
You can use the code below to get the above table.

<?php
echo '<table border="1">';
echo '<tr><th>Value of variable ($var)</th><th>isset($var)</th><th>empty($var)</th><th>is_null($var)</th></tr>';
$var = '';
echo '<tr><td>"" (an empty string)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = ' ';
echo '<tr><td>" " (space)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = FALSE;
echo '<tr><td>FALSE</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = TRUE;
echo '<tr><td>TRUE</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = array();
echo '<tr><td>array() (an empty array)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = NULL;
echo '<tr><td>NULL</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = '0';
echo '<tr><td>"0" (0 as a string)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = 0;
echo '<tr><td>0 (0 as an integer)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = 0.0;
echo '<tr><td>0.0 (0 as a float)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

unset($var); // doing this just as a precaution, to make sure $var is actually not defined.
$var;
echo '<tr><td>var $var; (a variable declared, but without a value)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = '\0';
echo '<tr><td>NULL byte ("\ 0")</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

echo '</table>';
?>
  1. Hi, would you mind to explain what’s the different between NULL and “” (empty string). If a form field is leave blank, will it return NULL or “”? Thanks BTW..

    • A variable is NULL if it has no value, and points to nowhere in memory.
      empty() is more a literal meaning of empty, e.g. the string “” is empty, but is not NULL.

      If a form field is left blank it will return “” i.e. is empty string.

      • Thanks for your fast reply.

        One more, in form validation why we need to do isset() check before empty() because what I concern is we want to check either the field is leave blank or has value, isn’t it?

        Hope you will solve my confusion ;)

        • Yes we use isset() to see if the variable exists and then use empty() to check if the variable has value or not. If we don’t use isset() and directly use empty(), there may be cases (e.g. checkboxes*) when the variable does not exists and can generate warnings/errors.

          *Checkboxes if not checked are not submitted when the form is submitted.

        • This was usefult thank you, but I’d also like to ask about the isset() check since in PHP manual it says that it also checks if a variable isn’t set(so it doesn’t exist if I’m right, I bad at ANY type of terminology) just as isset().

          Also they say it’s better to use it instead of isset() in some cases as unlike that empty() doesn’t generate an error.
          PHP manual:empty()
          “No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.”

          Any thoughts on that? I’ll be checking back from time to time and thank you for the answer up front. :)

          • Hi Gaboris,

            I am not sure I understand your question.
            Yes, isset() checks whether a variable exists or not. Also, do you mean to say that instead of “empty()” we can use ” !isset($var) || $var == false”?

          • Nono, I meant the other way around. Maybe I was the one who misunderstood you in the first place, but I think you said that empty() may give an error if the variable doesn’t exist, but in the manual they say it’s never gives an error.

            MAYBE I misunderstood both sides, but it’s better to ask. :)

          • I think I was not very clear in my earlier explanation. Yes empty() does not generates errors/warnings. So in my explanation the warnings are not generated by empty(). If you try to use a variable like $test['value'], and if $test['value'] does not exists, there will be a undefined variable notice.

          • AHH so you meant that an unset variable could go past empty() without us noticing and then cause an error. Okay then I get it. I just didn’ think of this since I use empty() exactly to find unset variables since those give back an error as well so I just set my checks up to catch those as well. :)

  2. Very good explanation! Helpful for clearing the thoughts.

    I have just one comment here – You probably need to unset($var) before testing ‘$var; (a variable declared, but without a value)’. =)

  3. Thanks a lot for the table. I reference it often.

  4. Thank you a lot. Very useful explanation. Exactly what I was searching for. Perfect job.

  5. For var $var; (a variable declared, but without a value) I get:
    $s;
    echo (“isset: ” . isset($s));
    echo “\n”;
    echo (“empty: ” . empty($s));
    OUTPUTS:
    isset:
    empty: 1

    So it isn’t set which would make intuitive sense since we’ve declared but it’s uninitialized. This is different from your table.

  6. is_null() is not just opposite of isset(). isset() can be applied to unknown variables, but is_null() only to declared variables.

  7. ”,
    ‘” ” (space)’ => ‘ ‘,
    ‘FALSE’ => FALSE,
    ‘TRUE’ => TRUE,
    ‘array() (an empty array)’ => array(),
    ‘NULL’ => NULL,
    ‘”0″ (0 as a string)’ => ‘0’,
    ‘0 (0 as a integer)’ => 0,
    ‘0.0 (0 as a float)’ => 0.0,
    ‘NULL byte (“\ 0″)’ => ”
    );
    ?>

    Value of variable ($var)
    isset($var)
    empty($var)
    is_null($var)

    $variable): ?>

    (a variable declared, but without a value)

  8. Thanks alot. I have learned so many things from your post.
    I may translate it into Turkish and link it to this post , do you allow?

    Regards.

  9. Thanks alot for your report!
    Really save my day!

  10. I was looking for just this. I’m making a website in PHP to manage my deployed applications since it’s such a hassle to do it manually. Your post was very helpful as it saved me from having to look all over the place to find a simple answer. Thanks!

  11. Also, regarding the top comment about blank form fields, don’t they just not submit? I created a form in HTML and named the text input “appName”. Then I used:

    $appName = $_POST['appName'];

    if (isset($appName)) {
    echo(“Not set”);
    } else {
    echo($appName);
    }

    When I leave the text field blank, it echos “Not set”. When I fill it in, it echos the content of the text field.

  12. The matter discussed in this post is really basic logic that can be unambiguously deduced by the very php manual for the functions themselves (as they are cited at the beginning). Reading the comments and the type and number of amendments to the original post solidifies my opinion on the majority of such blogs and posts where the author doesn’t have a clue or deep understanding of the (otherwise simple) matter he is writing about. Still, there are incorrect assertions… even on this basic language matter.
    Please, at least test thoroughly and learn the matter well before writing down such posts to the open internet. Stay away from amateur-ish and deceiving posts like this. Well, I’m sure my post won’t be approved and published.
    Virendra, good intentions but please make more educated and accurate/tested posts. Unless you want to look like an amateur to the public of course.

    • Hi Toe,

      Thanks for the comment. Yes, I did made a mistake in the post and that’s the reason I had to edit it. I had tested the post and have also included the code I had used, however made the mistake when I was formatting the post which I did not notice at first.

      I always test all my posts and try to add a working example so that whoever is reading my posts can also test it themselves.

      Since this happened I have starting checking my posts multiple times before posting as I don’t want to post incorrect information.

      • Hi Virendra,

        Please ignore Teo Teo’s judgmental and negative comments.

        This post has been very helpful to me and many other people. Ignore the haters and please continue doing good work.

        Best,

        Albert

  13. You are awesome. Thanks for explaning.

  14. This is good article. Thanks.

  15. Good article, one scenario missing from your test script is that of an undeclared variable. Below is sample code that should be on the first row (before you declare the variable).

    echo ‘Undeclared Variable';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;

    Note that is_null($var) when $var is not declared will throw a warning, and it will also throw an undefined variable error on (i.e. $var; ) using is_null().

    Ironically, the undeclared variable seems to be the most common problem I run into when trying to clean up error logs ;)

  16. Awesome Post thanks

  17. How about if ($var) does this equivalent to isset() ?

  18. Hi. I do isset($_SESSION['some_var']). This variable is not set at all, but I don’t get false back. Just problably an empty string.

    When I explicitly unset it, then I get false back.

    Why this happens? I expect to get false, if the variable is not set.

    Thanks

  19. It would be useful to also compare with: if ($var) {}

    I have amended the table writing code:

    <?php
    echo '';
    echo ‘Value of variable ($var)isset($var)empty($var)is_null($var)if ()';
    $var = ”;
    echo ‘”” (an empty string)';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = ‘ ‘;
    echo ‘” ” (space)';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = FALSE;
    echo ‘FALSE';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = TRUE;
    echo ‘TRUE';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = array();
    echo ‘array() (an empty array)';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = NULL;
    echo ‘NULL';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = ‘0’;
    echo ‘”0″ (0 as a string)';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = 0;
    echo ‘0 (0 as an integer)';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = 0.0;
    echo ‘0.0 (0 as a float)';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    unset($var); // doing this just as a precaution, to make sure $var is actually not defined.
    $var;
    echo ‘var $var; (a variable declared, but without a value)';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    $var = ”;
    echo ‘NULL byte (“\ 0″)';
    var_dump(isset($var));
    echo ”;
    var_dump(empty($var));
    echo ”;
    var_dump(is_null($var));
    echo ”;
    echo ($var) ? ‘true’ : ‘false';
    echo ”;

    echo ”;
    ?>

  20. This is on php.net – “Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.”

    That means we can use empty() to check whether variable exists and/or it has any value in it.

  21. Helpful explanation. Thank you.

  22. Forgive me but I have been so used to :

    if A$=”” then ‘do something with it.

    or

    if A$”” then ‘do something with it.

    Having added commands more than I am used to is making PHP slightly difficult as in $A= over the A$= yet I am sure there are reasons for this.

    There is confusion not fully cleared up with the php section when a programmer is not familiar with new commands in place of what they have been used to.

    This thread almost clears things up for me.. at least I now know isset() can determine if a variable is not set – so it can be dealt with, while empty() will check the item yet not fully report potential problem down the line.

    The demand for the ! in front is very curious for me, but I will discover it’s necessity from the manual.

    As far as this thread goes.. I am most appreciative it is available.

    Thank you.

  23. Thanks bro! This is a useful yet concise article that will be part of my PHP “refer once in a while” bookmarks

  24. no wonder i got stack on problem with PHP , lololol…. THANKS BUD

  25. Can you add an empty object to this table? empty($empty_object) === false for some reason.

Leave a Reply