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

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

Leave a Comment