note 121592 deleted from function.array-key-exists by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 20:44:44 +0000 |
| Subject: | note 121592 deleted from function.array-key-exists by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218767@lists.php.net to get a copy of this message | ||
Note Submitter: falundir at gmail dot com
----
This function will return NULL when second argument is not an array. So it will bite you when using
it like that:
<?php
$not_an_array = null;
if (array_key_exists($key, $not_an_array) === false) {
// the array doesn't have the key
return;
}
// happily proceed - surely the array does have the key (BUT IT DOESN'T!)
?>
That's because the condition null === false fails.
Instead you should use:
<?php
if (array_key_exists($key, $not_an_array) == false) {
return;
}
// or
if (!array_key_exists($key, $not_an_array)) {
return;
}
// or
if (is_array($not_an_array) === false || array_key_exists($key, $not_an_array) === false) {
return;
}
?>