94

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
  • 143,130
  • 81
  • 406
  • 459
kijin
  • 8,702
  • 2
  • 26
  • 32
  • 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

124

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
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173
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.

alex
  • 479,566
  • 201
  • 878
  • 984
Craige
  • 2,882
  • 2
  • 20
  • 28
  • 2
    Thanks for explaining compare-by-value vs. compare-by-type. – kijin Jan 11 '11 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 Mar 29 '17 at 15:11
19

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

Ish
  • 28,486
  • 11
  • 60
  • 77
  • 3
    and while === may be faster at first, PHP optimiser should remedy that. – foo Jan 11 '11 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 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
  • They are not exactly the same, one is a function with call back option, the other is a straight comparison on a value – James Oct 22 '21 at 15:17
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
  • 31,486
  • 7
  • 73
  • 96
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;
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • 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. – Á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
2

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

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
  • 4
    Care to update your answer to cite some logical or technical reasons? As it stands it's simply an opinion – James Oct 22 '21 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 ===).

Ogier Schelvis
  • 602
  • 12
  • 22
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
  • 33,559
  • 24
  • 104
  • 119
  • 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 Oct 22 '21 at 15:16