Lesson 4.4. isset vs empty vs is_null

How to test if a variable exists

In PHP, variables can provide from external sources. It is frequently usefull to check if the variable content is set or even if the variable exists or not before processing its value. The follwing table gather the main fonctions or tests and detailed their outputs:

Function Do no exist Empty NULL 0 '' or "" FALSE Undefined Other value
empty ($variable) TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
isset ($variable) FALSE FALSE FALSE FALSE TRUE TRUE - * TRUE
is_null ($variable) TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE
$variable != NULL FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
$variable !== NULL FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
$variable != FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
$variable !== FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE

* Calling the function isset(undefined) creates a PHP error.

Proof by example

See also


Last update : 09/17/2022