How do the following two function calls compare:
isset($a['key']) array_key_exists('key', $a)
Difference between isset() and array_key_exists() Function: The main difference between isset() and array_key_exists() function is that the array_key_exists() function will definitely tells if a key exists in an array, whereas isset() will only return true if the key/variable exists and is not null.
The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
If you want to know whether the array is defined at all, use isset($array) . If you want to know whether a particular key is defined, use isset($array[$key]) .
The isset function in PHP is used to determine whether a variable is set or not. A variable is considered as a set variable if it has a value other than NULL. In other words, you can also say that the isset function is used to determine whether you have used a variable in your code or not before.
array_key_exists
will definitely tell you if a key exists in an array, whereas isset
will only return true
if the key/variable exists and is not null
.
$a = array('key1' => 'フーバー', 'key2' => null); isset($a['key1']); // true array_key_exists('key1', $a); // true isset($a['key2']); // false array_key_exists('key2', $a); // true
There is another important difference: isset
doesn't complain when $a
does not exist, while array_key_exists
does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With