note 90687 deleted from function.array-key-exists by crell
| From: | crell@php.net | Date: | Wed, 29 Jun 2022 20:46:38 +0000 |
| Subject: | note 90687 deleted from function.array-key-exists by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218778@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at oehoeboeroe dot nl
----
The way array_key_exists handles null, float, boolean, and 'integer-representing string'
keys is inconsistent in itself and, in the case of bool and float, with the way these are converted
when used as array offset.
<?php
$array = array(null => 1, false => 2, true => 3, 4.6 => 4, "08" => 5,
"8" => 6);
var_export($array);
echo "\nnull is " . (array_key_exists(null, $array) ? '' : 'not ') .
"a key.\n";
echo 'false is ' . (array_key_exists(false, $array) ? '' : 'not ') .
"a key.\n";
echo 'true is ' . (array_key_exists(true, $array) ? '' : 'not ') .
"a key.\n";
echo '4.6 is ' . (array_key_exists(4.6, $array) ? '' : 'not ') .
"a key.\n";
echo '"08" is ' . (array_key_exists("08", $array) ? '' :
'not ') . "a key.\n";
echo '"8" is ' . (array_key_exists("8", $array) ? '' :
'not ') . "a key.\n";
?>
Output:
array (
'' => 1,
0 => 2,
1 => 3,
4 => 4,
'08' => 5,
8 => 6,
)
null is a key.
false is not a key.
true is not a key.
4.6 is not a key.
"08" is a key.
"8" is a key.
Well, and you get this warning three times (on the bools and the float, but not on the null):
Warning: array_key_exists() [function.array-key-exists]: The first argument should be either a
string or an integer in /var/www/php/test.php on line 6