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 PHP versions:

  • PHP 7.4.13
  • PHP 7.3.17
  • PHP 7.2.13
  • PHP 7.0.9
  • PHP 5.6.19
  • PHP 5.5.33

Related posts:

  1. How to apply a function to every array element in PHP
  2. more .htaccess tips
  3. How to sort a multi-dimension array by value in PHP
  4. Getting real client IP address in PHP

60 thoughts on “PHP isset() vs empty() vs is_null()”

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

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

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

        2. 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. 🙂

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

          2. 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. 🙂

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

          4. 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. 🙂

  1. 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)’. =)

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

      1. Is it worth adding 1 more line to the table t clarify a few of these questions:

        An unset variable or non-existent property of an object:
        isset = bool(false),
        empty = bool(true),
        is_null = [PHP Notice: Undefined variable]… which at runtime apart from raising the notice is treated as returning bool(true).

        (php 5.5.9)

  3. ”,
    ‘” ” (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)

  4. 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!

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

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

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

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

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

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

  9. 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 ”;
    ?>

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

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

  12. Don’t ever use is_null for Singleton, it will generate exception
    Note:
    public static function getInstance()
    {
    $class = get_called_class();
    if (is_null(self::$_instances[$class])) {
    self::$_instances[$class] = new $class();
    }
    return self::$_instances[$class];
    }

  13. isset() and empty() are not a functions but language constructs.
    If you will try to test nonexistent variable by empty() you will get an notice that variable is not set, so you cannot test unset variables with that construct.

  14. Another “cheap” way of doing so:

    if (@$this_var==”) echo “Hmmm. Must either be unset, or empty???”;

    Though I do not like doing things this way, it does seem to work, especially against “E-Warning” errors.

    Nice explanations, BTW.

    – Jim

    1. Hey Gayan,
      When used empty() on “0” as a string it is false, but when used on int it is true. Can you provide the code that you are using to test this. You can look download the demo code and test it. Let me know if you see different results and if so please also provide your php version.

Leave a Reply