isset() vs array_key_exists()
For most of my PHP career, I’ve used isset()
and empty()
to determine if an array has a certain key.
But in PHP there are other ways that you can check for variables in arrays, and some of them are also in_array
and array_key_exists
In this article, we will cover the difference between isset()
and array_key_exists
.
isset()
is a built-in function in PHP that determines if a variable is set and not null. It returns true
if the variable exists and has a value other than null
, and false
otherwise.
The syntax for isset()
is as follows:
isset($var1, $var2, $var3, ...);
array_key_exists()
is a built-in function in PHP that is used to check if a specified key or index exists in an array. It returns true
if the key/index exists in the array, and false
otherwise.
The syntax for array_key_exists()
is as follows:
array_key_exists($key, $array);
Note that there are some key differences between these two, for example:
isset()
is a language construct in PHP that is used to check if a variable is set and notNULL
. It can be used to check if an array key exists and has a value.array_key_exists()
, on the other hand, is a function specifically designed to check if a given key exists in an array, regardless of its value.isset()
can be used with both indexed arrays and associative arrays, whereasarray_key_exists()
is specifically designed to work with associative arrays, i.e., arrays with string keys.isset()
is slightly faster thanarray_key_exists()
because it is a language construct in PHP, whilearray_key_exists()
is a built-in function.
A photo is worth a thousand words, an example is worth a thousand photos 😅.
Let’s suppose we have the following array:
$array = [
'name' => 'Antonio',
'age' => 32,
'vip' => false,
'credits' => 0,
'city' => '',
'connections' => null,
'active' => true,
];
And we var_dump()
the result, the output would be:
// isset()
'isset($array[name])' => boolean true
'isset($array[age])' => boolean true
'isset($array[vip])' => boolean true
'isset($array[credits])' => boolean true
'isset($array[city])' => boolean true
'isset($array[connections])' => boolean false // <==
'isset($array[active])' => boolean true
'isset($array[missing_key])' => boolean false
// array_key_exists()
'array_key_exists('name', $array)' => boolean true
'array_key_exists('age', $array)' => boolean true
'array_key_exists('vip', $array)' => boolean true
'array_key_exists('credits', $array)' => boolean true
'array_key_exists('city', $array)' => boolean true
'array_key_exists('connections', $array)' => boolean true // <==
'array_key_exists('active', $array)' => boolean true
'array_key_exists('missing_key', $array)' => boolean false
As you can see, for connections
key, the isset()
and array_key_exists()
return different results, because array_key_exists() only checks if the key of the array exists, while the isset() checks that the key exists and has a value different from NULL.
The choice between isset()
and array_key_exists()
depends on the specific use case and requirements of your code. Here are some guidelines to help you decide when to use each function:
- Use
isset()
when you want to check if a variable or an array key is set and notNULL
. - Use
array_key_exists()
when you specifically want to check if a given key exists in an associative array, regardless of its value.
Feel free to Subscribe for more content like this 🔔, clap 👏🏻 , comment 💬 and share the article with anyone you’d like
And as it always has been, I appreciate your support, and thanks for reading.