How do the following two function calls compare:
isset($a['key'])
array_key_exists('key', $a)
How do the following two function calls compare:
isset($a['key'])
array_key_exists('key', $a)
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.
isset()
because I think that NULL should always mean exactly the same as NO VALUE or NOT SET, to avoid ambiguousness. –
Rhatany isset()
because is faster! See https://mcmap.net/q/81472/-what-39-s-quicker-and-better-to-determine-if-an-array-key-exists-in-php or Alex link above or Patrick below, all confirm that is faster. –
Sainfoin isset
due to the fact that one does not really know if the index exists or not. –
Ammonal isset
over array_key_exists
, when applicable. –
Rhatany null
may be a regular value, but it means "no value" or "none of the above". null
is not a useful value in itself, since it can only have exactly one value: null
. As such it can only have exactly one meaning: "nothing". So if isset
says "you have no value here", then it should be practically irrelevant whether that's because there's no such variable (i.e. no value) or because the variable is null
(i.e. no value). That's the ideal you should strive for, but the practice can certainly be more nuanced. –
Glairy foobar
if character 2 and 4 are the same. –
Cense isset
should not log anything, that's very unusual, I'd ask for code to reproduce this. If you don't want to use it because you need to distinguish between null
and nonexistent, that's perfectly legitimate. –
Glairy isset()
is wrong in this case, and should be altered to actually test if a variable exists. isset()
should not be concerned about the value. –
Plimsoll isset
because I want to use the variable if it's available and has a value. But, in certain cases, null is a valid value. For instance, distinguishing between true (positive), false (negative), and null (not set). I also use array_key_exists
somewhat frequently to check if a key is present in a set of options, and then set a default value if not present so later code won't trigger errors for undefined keys. –
Interlocutory if (isset($data[$name]) || array_key_exists($name, $data))
And the last comment of this answer clarifies. The line makes sure the variable $data
exists before testing it's subvalues exist. –
Pickerel isset($data) && array_key_exists($name, $data)
, otherwise it's pretty pointless. Preferably you wouldn't even need isset
at all if you initialized your variables properly. –
Glairy isset($webhook['leads']['update'][0]['id'])
without worrying that even the $webhooks
variable can be non-existing. Missing something like this in JavaScript (remember those if (webhook && webhook.leads && webhook.leads.update && webhook.leads.update[0] && webhook.leads.update[0].id)
— when you have to check each level in order to avoid errors). –
Nonprofessional array_key_exists
and isset( $array[$index] )
is O(n) but really close to O(1), though isset()
is faster. –
Terramycin Between array_key_exists
and isset
, though both are very fast [O(1)]
, isset
is significantly faster. If this check is happening many thousands of times, you'd want to use isset
.
It should be noted that they are not identical, though -- when the array key exists but the value is null
, isset
will return false
and array_key_exists
will return true
. If the value may be null
, you need to use array_key_exists
.
As noted in comments, if your value may be null
, the fast choice is:
isset($foo[$key]) || array_key_exists($key, $foo)
array_key_exists
to isset
will give you a 297 seconds speed improvement. –
Fetter if (isset($foo[$key]) || array_key_exists($key, $foo))
should give the same results as array_key_exists
but faster if you know that your data will have few keys pointing to null
–
Propagate array_key_exists()
: 205 ms is_set()
: 35ms isset() || array_key_exists()
: 48ms –
Spoofery isset($foo[$key]) && $foo[$key] !== null
might be better depending on your use case –
Deckard isset($foo[$key])
implies $foo[$key] !== null
, so this test makes no sense! –
Poster isset($foo[$key]) || array_key_exists($key, $foo)
–
Iceman The main difference when working on arrays is that array_key_exists
returns true
when the value is null
, while isset
will return false
when the array value is set to null
.
See the PHP documentation for isset()
.
isset
returns false and not null. –
Cleave Answer to an old question as no answer here seem to address the 'warning' problem (explanation follows)
Basically, in this case of checking if a key exists in an array, isset
and array_key_exists
So how do we check if a key exists which value may be null in a variable
without getting a warning, without missing the existing key when its value is null (what were the PHP devs thinking would also be an interesting question, but certainly not relevant on SO). And of course we don't want to use @
isset($var[$key]); // silent but misses null values
array_key_exists($key, $var); // works but warning if $var not defined/array
It seems is_array
should be involved in the equation, but it gives a warning if $var
is not defined, so that could be a solution:
if (isset($var[$key]) ||
isset($var) && is_array($var) && array_key_exists($key, $var)) ...
which is likely to be faster if the tests are mainly on non-null values. Otherwise for an array with mostly null values
if (isset($var) && is_array($var) && array_key_exists($key, $var)) ...
will do the work.
The PHP function array_key_exists()
determines if a particular key, or numerical index, exists for an element of an array. However, if you want to determine if a key exists and is associated with a value, the PHP language construct isset()
can tell you that (and that the value is not null
). array_key_exists()
cannot return information about the value of a key/index.
Function isset()
is faster, check http://www.php.net/manual/en/function.array-key-exists.php#82867
isset()
will return false if the variable is set but to the value of "null
" –
Sipple Complementing (as an algebraic curiosity) the @deceze answer with the @
operator, and indicating cases where is "better" to use @
... Not really better if you need (no log and) micro-performance optimization:
array_key_exists
: is true if a key exists in an array; isset
: is true
if the key/variable exists and is not null
[faster than array_key_exists]; @$array['key']
: is true
if the key/variable exists and is not (null
or '' or 0); [so much slower?] $a = array('k1' => 'HELLO', 'k2' => null, 'k3' => '', 'k4' => 0);
print isset($a['k1'])? "OK $a[k1].": 'NO VALUE.'; // OK
print array_key_exists('k1', $a)? "OK $a[k1].": 'NO VALUE.'; // OK
print @$a['k1']? "OK $a[k1].": 'NO VALUE.'; // OK
// outputs OK HELLO. OK HELLO. OK HELLO.
print isset($a['k2'])? "OK $a[k2].": 'NO VALUE.'; // NO
print array_key_exists('k2', $a)? "OK $a[k2].": 'NO VALUE.'; // OK
print @$a['k2']? "OK $a[k2].": 'NO VALUE.'; // NO
// outputs NO VALUE. OK . NO VALUE.
print isset($a['k3'])? "OK $a[k3].": 'NO VALUE.'; // OK
print array_key_exists('k3', $a)? "OK $a[k3].": 'NO VALUE.'; // OK
print @$a['k3']? "OK $a[k3].": 'NO VALUE.'; // NO
// outputs OK . OK . NO VALUE.
print isset($a['k4'])? "OK $a[k4].": 'NO VALUE.'; // OK
print array_key_exists('k4', $a)? "OK $a[k4].": 'NO VALUE.'; // OK
print @$a['k4']? "OK $a[k4].": 'NO VALUE.'; // NO
// outputs OK 0. OK 0. NO VALUE
PS: you can change/correct/complement this text, it is a Wiki.
@
operator. –
Thimbu $x = @$_GET['x'];
, than $x = array_key_exists('x',$_GET)? $_GET['x']: '';
. See this question in order to decide by yourself. –
Sainfoin @
operator, especially when dereferencing arrays. isset()
or array_key_exists()
communicate the intent of the code, and don't misuse an already inherently misusable operator. Obviously you don't need to convince me, but if you can provide an instance in which the @
operator is measurably better in performance, or communicates the intent of the code more succinctly than an alternative, I'll gladly change my tone. –
Thimbu error_reporting(0)
. –
Lemuelah @
occurrences and when not). –
Sainfoin The two are not exactly the same. I couldn't remember the exact differences, but they are outlined very well in What's quicker and better to determine if an array key exists in PHP?.
The common consensus seems to be to use isset whenever possible, because it is a language construct and therefore faster. However, the differences should be outlined above.
© 2022 - 2024 — McMap. All rights reserved.