150

Possible Duplicate:
What's the difference between is_null($var) and ($var === null)?

PHP has two (that I know of, and three if you count isset()) methods to determine if a value is null: is_null() and === null. I have heard, but not confirmed, that === null is faster, but in a code review someone strongly suggested that I use is_null() instead as it is specifically designed for the null-evaluation purpose. He also started talking about math or something.

Anyway, the fact that is_null() is apparently slower also leads me to believe that it's doing more than === null does and is probably preferred. Is there any reason to use one or the other? Is one always preferred? What about isset()?

As an addendum to possibly not get this question closed, what about isset() vs. is_null()? It seems that all isset() will do is suppress the notice, so unless you actually want a notice for an undefined variable, any reason to use is_null() instead? How about if you know the variable is initialized at the time?

Finally, is there any mathematical reason to prefer is_null() over === null? Something about null not being comparable?

Community
  • 1
  • 1
Explosion Pills
  • 176,581
  • 46
  • 285
  • 363
  • 2
    Good question, but as far as I've learned, these kind of "improvements" are so minimal it does not matter what way you write it. Anyways, waiting for someone with competence to answer this, I'd like to know too. – OptimusCrime Nov 22 '11 at 15:00
  • 2
    Whoever voted to reopen should point out why this question is either not a duplicate of what it was closed with or why it should be allowed to co-exist. Also consider flagging it for merging. – Gordon Nov 22 '11 at 16:56
  • 3
    Performance-wise: PHP 7 changes the old wisdom: ``===`` is no longer faster than ``is_null``. – Gogowitsch Nov 13 '17 at 15:57

7 Answers7

210

There is absolutely no difference in functionality between is_null and === null.

The only difference is that is_null is a function and thus

  1. is marginally slower (function call overhead)
  2. can be used as a callback, e.g. array_map('is_null', $array).

Personally, I use null === whenever I can, as it is more consistent with false === and true === checks.

If you want, you can check the code: is_identical_function (===) and php_is_type (is_null) do the same thing for the IS_NULL case.


The related isset() language construct checks whether the variable actually exists before doing the null check. So isset($undefinedVar) will not throw a notice.

Also note that isset() may sometimes return true even though the value is null - this is the case when it is used on an overloaded object, i.e. if the object defines an offsetExists/__isset method that returns true even if the offset is null (this is actually quite common, because people use array_key_exists in offsetExists/__isset).

NikiC
  • 95,987
  • 31
  • 182
  • 219
  • Good point on the speed difference. – Robert Rocha Apr 06 '18 at 12:58
  • 10
    According to a comment in the documentation, `is_null()` is now marginally faster in PHP7 than `=== NULL` but the difference between the two is far smaller than that of previous versions and not worth worrying about. – Martin James Jun 17 '18 at 12:30
  • Ive tested both using a for-loop with 100,000,000 iterations on PHP `7.4`. The difference is negligible. Just pick one and use it. – Flame May 07 '21 at 11:47
17

As stated by others, there is a time difference between using === and is_null(). Did some quick testing and got these results:

<?php

//checking with ===
$a = array();
$time = microtime(true);
for($i=0;$i<10000;$i++) {
    if($a[$i] === null) {
         //do nothing
    }
}
echo 'Testing with === ', microtime(true) - $time, "\n";

//checking with is_null()
$time = microtime(true);
for($i=0;$i<10000;$i++) {
    if(is_null($a[$i])) {
         //do nothing
    }
}
echo 'Testing with is_null() ', microtime(true) - $time;
?>

Gives the results

Testing with === 0.0090668201446533

Testing with is_null() 0.013684034347534

See the code in action

jprofitt
  • 10,710
  • 4
  • 33
  • 45
  • 35
    You're doing 10000 operations in a loop and the time difference turns out to be less than 5 milliseconds. It doesn't matter. It will never matter. Nobody trying to improve their application's performance has ever declared "curses, if only I could shave half a microsecond off this null check!", and nobody ever will. – Mark Amery Jun 20 '15 at 23:17
  • 17
    @MarkAmery I am going for whatever performs better at same cost, and always will. Care less is worse than care more in a 50k+ files app. – Aleksandr Makov Aug 17 '15 at 13:21
  • 10
    I think the point here is there's no need to refactor if you've already used is_null, but there's no point in using it _from now on_, if you get my drift. – PaulSkinner Dec 04 '15 at 11:59
  • There are cases where simple operations are repeated a lot. E.g. a parser that looks at each character in a long file. – donquixote Feb 21 '16 at 01:46
  • 3
    Program execution time tends to follow a Pareto distribution i.e. ~20% of the codebase is responsible for ~80% of the runtime (when its not 10/90 or even 1/99). So, ya, this kind of microoptimization won't usually buy you much. – Marcel Hernandez May 31 '16 at 10:21
  • 1
    @donquixote - FWIW, Mark Amery's point is that the difference here is so miniscule, that the cases where it would matter are really really rare. – ToolmakerSteve Aug 17 '16 at 18:26
  • 1
    The speed leverage was very low. But now in PHP 7.1 they both execute at the same speed. – Jack B Nov 15 '18 at 11:54
6

They all have their places, though only isset() will avoid undefined variable warnings:

$ php -a
Interactive shell

php > var_dump(is_null($a));
PHP Notice:  Undefined variable: a in php shell code on line 1
bool(true)
php > var_dump($a === null);
PHP Notice:  Undefined variable: a in php shell code on line 1
bool(true)
php > var_dump(isset($a));
bool(false)
php >
Marc B
  • 340,537
  • 37
  • 382
  • 468
6

I'm not able to say wether it's better to use is_null or === null. But be aware when using isset on arrays.

$a = array('foo' => null);

var_dump(isset($a['foo'])); // false
var_dump(is_null($a['foo'])); // true
var_dump(array_key_exists('foo', $a)); // true
mAu
  • 1,950
  • 1
  • 13
  • 27
  • +1 for array_key_exists('foo', $a), the use is finding keys that are set to null because for some reason is_null($a['foo']) returns true for undefined properties AND null keys????? – Necro Dec 10 '18 at 05:03
5

You need isset() if the variable is possibly not defined. It returns false when the variable is not defined or === null (yes, it's that ugly). Only isset() and empty() do not raise an E_NOTICE if the variable or array element does not exist.

There is not really a difference between is_null and === null. I think === is much nicer but when you e.g. need to use call_user_func for some dubious reason, you'd have to use is_null.

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
5

=== and is_null is the same.

According to this comment is_null is only 250ns slower. I think because a function is slower than an operator.

PiTheNumber
  • 20,216
  • 13
  • 96
  • 165
  • On PHP 7.2.19 (and an i5-8265U), is_null is actually faster. But `null ===` is only 1 nanosecond slower. Don't bother with the difference. – Henk Poley Aug 21 '19 at 10:08
  • For reference, ~1 ns is around 4 to 5 clock cycles at 3.9GHz. Given that Whiskey Lake seems to be able to retire 6 instructions per cycle, that's still up to 24 to 28 instructions. But that's wayyy beyond the detail of the underlying hardware that you have in PHP (which tends to be >10x-30x slower than C anyways). – Henk Poley Aug 22 '19 at 09:43
2

The PHP documentation has a good discussion and experiments on is_null, === null, isset. Especially read the comment section.

Shakti Singh
  • 77,873
  • 18
  • 129
  • 147