note 90687 added to function.array-key-exists

From: Date: Mon, 04 May 2009 20:09:06 +0000
Subject: note 90687 added to function.array-key-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-154206@lists.php.net to get a copy of this message
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 ---- Server IP: 213.249.64.162 Probable Submitter: 82.92.197.212 ---- Manual Page -- http://www.php.net/manual/en/function.array-key-exists.php Edit -- https://master.php.net/note/edit/90687 Del: integrated -- https://master.php.net/note/delete/90687/integrated Del: useless -- https://master.php.net/note/delete/90687/useless Del: bad code -- https://master.php.net/note/delete/90687/bad+code Del: spam -- https://master.php.net/note/delete/90687/spam Del: non-english -- https://master.php.net/note/delete/90687/non-english Del: in docs -- https://master.php.net/note/delete/90687/in+docs Del: other reasons-- https://master.php.net/note/delete/90687 Reject -- https://master.php.net/note/reject/90687 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154206) next »