isset() will return:
-true if the key exists and the value is != NULL
-false if the key exists and value == NULL
-false if the key does not exist
array_key_exists() will return:
-true if the key exists
-false if the key does not exist
So, if your value may be NULL, the proper way is array_key_exists
. If your application doesn't differentiate between NULL and no key, either will work, but array_key_exists
always provides more options.
In the following example, no key in the array returns NULL, but so does a value of NULL for a given key. That means it's effectively the same as isset
.
The null coalesce operator(??) wasn't added until PHP 7, but this works back to PHP 5, maybe 4:
$value = (array_key_exists($key_to_check, $things) ? $things[$key_to_check] : NULL);
as a function:
function get_from_array($key_to_check, $things)
return (array_key_exists($key_to_check,$things) ? $things[$key_to_check] : NULL);