What's the difference between is_null($var) and ($var === null)?
✅
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 ofis_null
and===null
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.
Both are exactly same, I use is_null
because it makes my code more readable