Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated our privacy policy to let you know that we use cookies to personalise content and ads. We also use cookies to analyse our traffic and we share information about your use of our site and application with our advertising and analytics partners. By using this website or our application you agree to our use of cookies. Learn more about the way this website uses cookies or remove this message.

You can read this article in: English :: Español :: русский

is_null vs empty vs isset... One lesson all PHP coders should learn

July 12, 2014 Difficulty: 25 / 50 Tweet
empty tin can

PHP has a lot of ways of dealing with variable checking. There are functions that check each type like is_array, is_object or is_bool and there are functions that can be used to check multiple conditions at once. Today we will be dealing with the differences between is_null(), empty() and isset(). First let's explain what each one does.


This function checks only whether a variable has a null value, but it doesn't cover for cases when that variable is undefined or for the cases when a value would evaluate to an empty value.

Let's have a look at a few examples:

    $myvar = NULL; is_null($myvar); // This is TRUE
    $myvar = 0; is_null($myvar); // This is FALSE
    $myvar = FALSE; is_null($myvar); // This is FALSE
    $myvar = '';  is_null($myvar); // This is FALSE
    is_null($some_undefined_var); //This is also TRUE ... but the script throws a notice because $some_undefined_var doesn't exist

Please note that a null variable is not the same as an undefined variable. Even if they both evaluate to NULL, PHP will through a notice saying: "Notice: Undefined variable: X in..."


This function checks whether a variable is defined (a.k.a exists) within your script. However, isset() also sees variables that have the null value as being not set (if you take the function wording literally).

This can be a little confusing but a few examples will show you exactly what I mean:

    $myvar = NULL; isset($myvar); // This is FALSE
    $myvar = 0; isset($myvar); // This is TRUE
    $myvar = FALSE; isset($myvar); // This is TRUE
    $myvar = '';  isset($myvar); // This is TRUE
    isset($some_undefined_var); // This is FALSE


This function checks whether a variable evaluates to what PHP sees as a "falsy"(a.k.a empty) value. This being said, a variable is empty if it's undefined, null, false, 0 or an empty string.

To better understand the way empty works... you should think about this function as being the same as: !isset($var) || $var==false.

Here are some examples

    $myvar = NULL; empty($myvar); // This is TRUE
    $myvar = 0; empty($myvar); // This is TRUE
    $myvar = FALSE; empty($myvar); // This is TRUE
    $myvar = '';  empty($myvar); // This is TRUE
    empty($some_undefined_var); // This is TRUE

To conclude, remember to ALWAYS do your variable checking. This is especially important in PHP which is not a strictly typed programming language so the programmer needs to pay special attention it.

comments powered by Disqus