86

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

Mark Amery
  • 110,735
  • 57
  • 354
  • 402
kijin
  • 8,250
  • 2
  • 24
  • 30
  • 6
    You would think the `===` operator would be faster since it's not an explicit function... but I've been surprised once or twice. – John Giotta Jan 11 '11 at 21:01

8 Answers8

104

is true

is false

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

Summary:♦️

  • empty is equivalent to ==null
  • is_null is equivalent to ===null
  • isset is inverse of is_null and ===null

An important point is empty and isset do not trigger a PHP warning if their parameter is an undefined variable. So if you expect that the variable or array index which you are testing upon are always defined, use the operator otherwise use the function.

PHPst
  • 14,868
  • 20
  • 85
  • 158
32

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.

alex
  • 438,662
  • 188
  • 837
  • 957
Craige
  • 2,793
  • 2
  • 17
  • 28
  • 2
    Thanks for explaining compare-by-value vs. compare-by-type. – kijin Jan 11 '11 at 21:21
  • 5
    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 Mar 29 '17 at 15:11
17

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

Ish
  • 24,560
  • 11
  • 57
  • 75
  • 3
    and while === may be faster at first, PHP optimiser should remedy that. – foo Jan 11 '11 at 21:22
  • 11
    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 Sep 28 '14 at 16:08
  • 5
    @goat, ish1301, Also, `is_null` could be [removed or redefined](http://php.net/manual/en/function.runkit-function-remove.php) entirely, whereas `=== null` will always work. – Pacerier Mar 09 '15 at 03: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. – kaan_a Jan 16 '20 at 11:49
7

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');
goat
  • 29,650
  • 7
  • 65
  • 92
5

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;
lonesomeday
  • 215,182
  • 48
  • 300
  • 305
  • 1
    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. – Álvaro González Oct 20 '17 at 08: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 Dec 05 '17 at 23:50
1

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

Michael Irigoyen
  • 21,233
  • 17
  • 82
  • 125
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 ===).

Ogier Schelvis
  • 532
  • 10
  • 19
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.

Floern
  • 31,495
  • 23
  • 98
  • 115