95

Is there any difference between this...

if (is_null($var)) {
    do_something();
}

and this?

if ($var === null) {
    do_something();
}

Which form is better when checking whether or not a variable contains null? Are there any edge cases I should be aware of? (I initialize all my variables, so nonexistent variables are not a problem.)

1
  • 6
    You would think the === operator would be faster since it's not an explicit function... but I've been surprised once or twice. Commented Jan 11, 2011 at 21:01

8 Answers 8

125

empty() and isset() do not trigger a PHP warning if their parameter is an undefined variable. In most cases, such a warning is desirable to pinpoint the bugs. So only use the functions if you believe your variable can be legitimately undefined. It normally happens with an array index.

is true

is false

        |  isset   | is_null | === null | == null | empty   |
|-------|----------|---------|----------|---------|---------|
| unset |    ❌   |    ✅   |    ✅    |    ✅  |    ✅   |
|  null |    ❌   |    ✅   |    ✅    |    ✅  |    ✅   |
|  true |    ✅   |    ❌   |    ❌    |    ❌  |    ❌   |
| false |    ✅   |    ❌   |    ❌    |    ✅  |    ✅   |
|     0 |    ✅   |    ❌   |    ❌    |    ✅  |    ✅   |
|     1 |    ✅   |    ❌   |    ❌    |    ❌  |    ❌   |
|    \0 |    ✅   |    ❌   |    ❌    |    ❌  |    ❌   |
|    "" |    ✅   |    ❌   |    ❌    |    ✅  |    ✅   |
|    [] |    ✅   |    ❌   |    ❌    |    ✅  |    ✅   |

Summary:🔸♦️🔸

  • empty is equivalent to == null
  • is_null is equivalent to === null
  • isset is inverse of is_null and === null
6
  • 19
    I really love this table. Commented Feb 19, 2014 at 8:58
  • 3
    it might make the table slightly more clear to have the first column named $v to represent some generic variable, and then the others isset($v), is_null($v), $v===null and so on
    – Brandin
    Commented Oct 19, 2014 at 18:30
  • @Brandin Thanks for the suggestion. Feel free to edit the answer. Commented Oct 20, 2014 at 5:35
  • 1
    isset will not complain about an undefined variable, it will return false. Using is_null or === null will result in a notice. Commented Jun 8, 2016 at 10:25
  • 1
    The misaligned columns in this table are causing me a lot of stress.
    – hackel
    Commented Oct 25, 2020 at 0:03
36

Provided the variable is initialized (which you did indicate - though I'm not 100% sure if this matters in this context or not. Both solutions might throw a warning if the variable wasn't defined), they are functionally the same. I presume === would be marginally faster though as it removes the overhead of a function call.

It really depends on how you look at your condition.

=== is for a strict data comparison. NULL has only one 'value', so this works for comparing against NULL (which is a PHP constant of the null 'value')

is_null is checking that the variable is of the NULL data type.

It's up to you which you choose, really.

2
  • 2
    Thanks for explaining compare-by-value vs. compare-by-type.
    – kijin
    Commented Jan 11, 2011 at 21:21
  • 7
    I prefer the is_null variant, because it is easy to accidentally use == null instead of === null. Since == null is the same as empty (see other answer), this introduces greater potential for programmer (especially junior and mid-level) error than a simple and readable is_null.
    – ryanm
    Commented Mar 29, 2017 at 15:11
20

Both are exactly same, I use is_null because it makes my code more readable

5
  • 3
    and while === may be faster at first, PHP optimiser should remedy that.
    – foo
    Commented Jan 11, 2011 at 21:22
  • 13
    I feel is_null is actually less clear. While it may read nicely, $v === null leaves no doubt that the comparison is done with strict semantics, but with is_null($v), some coders will wonder if it uses === or == semantics.
    – goat
    Commented Sep 28, 2014 at 16:08
  • 5
    @goat, ish1301, Also, is_null could be removed or redefined entirely, whereas === null will always work.
    – Pacerier
    Commented Mar 9, 2015 at 3:15
  • @Pacerier while your example is interesting the likelihood of someone installing the runkit pecl package, then setting it up to allow removing built in functions and then removing is_null, of all things, without replacing it with something is very close to nil, or null if you prefer. The only scenario I can think of where a person would want to remove is_null, is if they wanted to change the behavior of the null comparison globally. For example; returning false instead of true for unset; In this case it would actually only be possible if the code was written with is_null to begin with. Commented Jan 16, 2020 at 11:49
  • They are not exactly the same, one is a function with call back option, the other is a straight comparison on a value
    – James
    Commented Oct 22, 2021 at 15:17
6

I've just run a quick benchmark, testing a million iterations of each. is_null took 8 seconds to complete; === null took 1.

So a call to is_null is 0.000007s slower than a call to === on my computer.

I'd find something more useful to optimise.


My code:

<?php

$start = time();
$var = null;

for ($i = 1000000; $i--; ) {
    is_null($var);
}

echo time() - $start;

$start = time();

for ($i = 1000000; $i--; ) {
    $var === null;
}

echo time() - $start;
2
  • 2
    Interestingly, PHP/7 seems to have addressed this and performance is now almost identical; in fact, is_null() is slightly faster in my computer under 7.1.9 Win32. Commented Oct 20, 2017 at 8:31
  • @ÁlvaroGonzález I noticed the same thing. The difference is negligible between the two functions. (is_null: 0.54486592610677, === null: 0.7440135592506) at 10,000 operations. The test was ran 30 times, and these numbers are the average.
    – Justin E
    Commented Dec 5, 2017 at 23:50
6

If it seems redundant for php to have so many is_foo() type functions, when you can just use a standard comparison operators, consider programatically called functions.

$arrayOfNullValues = array_filter($myArray, 'is_null');
1
  • 1
    $arrayWithoutNullValues would store an array that has only null values, then. Commented Feb 28, 2012 at 16:00
2

I would use the built in PHP function over the operator comparison every time.

1
  • 4
    Care to update your answer to cite some logical or technical reasons? As it stands it's simply an opinion
    – James
    Commented Oct 22, 2021 at 15:14
1

One thing people often forget to mention in this discussion is that if you are all about strict type checking, is_null will help you to never make a typo in your comparison operators (== vs ===).

0
0

is_null($var) is about 14 times slower than $var===null... 37.8 ms vs. 2.6 ms.

But actually I don't know why.

1
  • 1
    Because it's a function and it has overhead (e.g. setting up the callback etc). However in PHP7 it's about the same (reports show the function is actually marginally faster)
    – James
    Commented Oct 22, 2021 at 15:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.