Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

like image 825
kijin Avatar asked Jan 11 '11 20:01

kijin


People also ask

What is the difference between NULL and empty in PHP?

The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .

What is the difference between $VAR and $$ var in PHP?

?> Difference between Both: The variable $var is used to store the value of the variable and the variable $$val is used to store the reference of the variable.

How check variable is empty or NULL in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.


2 Answers

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.

like image 170
Handsome Nerd Avatar answered Oct 13 '22 04:10

Handsome Nerd


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.

like image 37
Craige Avatar answered Oct 13 '22 04:10

Craige