PHP: Distinguish unset array element from null array element
Let’s execute following code:
The result:
index 0:
isset: bool(true)
is_null: bool(false)
index 1:
isset: bool(false)
is_null: bool(true)
index 2:
isset: bool(false)
is_null: bool(true)
index 3:
isset: bool(true)
is_null: bool(false) Notice, that for `$a[2]` as well as for `$a[1]` `is_null` and `isset` return true, so you can't distinguish between set variable with null value from completely unset variable. However you can use `array_key_exists` to do so:
This results with:
index 0:
isset: bool(true)
is_null: bool(false)
array_key_exists: bool(true)
index 1:
isset: bool(false)
is_null: bool(true)
array_key_exists: bool(true)
index 2:
isset: bool(false)
is_null: bool(true)
array_key_exists: bool(false)
index 3:
isset: bool(true)
is_null: bool(false)
array_key_exists: bool(true)
Hope this will help you. It took me a while to figure out.