#39764 [Fbk->Opn]: array_key_exists inconsistent behavior

From: Date: Thu, 07 Dec 2006 12:56:30 +0000
Subject: #39764 [Fbk->Opn]: array_key_exists inconsistent behavior
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-106059@lists.php.net to get a copy of this message
ID: 39764 User updated by: techtonik@php.net Reported By: techtonik@php.net -Status: Feedback +Status: Open Bug Type: Arrays related Operating System: Windows2000 PHP Version: 4.4.4 New Comment: If even experienced developers who write the manual tend to make this kind of "mistakes" then what should we expect from other users? From the other side if we assume that PHP is OO language then it should be indifferent to the type of object used as array key as long as the object can be hashed and compared to others. IIRC that is the way it works for Python and Java (toString() method). Previous Comments: ------------------------------------------------------------------------ [2006-12-07 12:35:30] tony2001@php.net Why do you promote bad code in the docs? This code sample is wrong and should be fixed in order to avoid using special types as array keys. ------------------------------------------------------------------------ [2006-12-07 12:27:01] techtonik@php.net For resource-dependent array data. Configuration or state information, whatever. For example - http://www.php.net/xml#AEN211968 ------------------------------------------------------------------------ [2006-12-07 10:50:55] tony2001@php.net Name at least one reason why do you need to use resource as array keys. >E_STRICT is turned off in PHP5 Yes, it can be turned off, as well as E_WARNING. ------------------------------------------------------------------------ [2006-12-07 10:41:54] techtonik@php.net "Expected result" and "Actual result" should be switched. Sorry for confusion. ------------------------------------------------------------------------ [2006-12-07 10:38:02] techtonik@php.net Description: ------------ Array indexes can be resources - in this case they are casted to int automatically without any warning (E_STRICT is turned off in PHP5). Hovewer, array_key_exists() throws warning if resource is passed as a key and fails to recognize the key. This leads to confusion and is illustrated below. More logical behavior is to make conversion automatically and display warning only in E_STRICT mode for PHP5. Yes, the problem persists in PHP5 as well. Reproduce code: --------------- <?php $depth = array(); $parser = xml_parser_create(); $depth[$parser] = "xxx"; echo "\n -".array_key_exists((int)$parser, $depth); echo "\n -".array_key_exists("" + $parser, $depth); echo "\n -".array_key_exists("" . $parser, $depth); echo "\n -".array_key_exists((int) $parser, $depth); echo "\n".$depth[$parser]; echo "\n".$depth[(int)$parser]; echo "\n"; $z = "" + $parser; var_dump($z); $z = "" . $parser; var_dump($z); $z = (string) $parser; var_dump($z); $z = strval($parser); var_dump($z); exit(); Expected result: ---------------- Warning: array_key_exists(): The first argument should be either a string or an integer in C:\php51\test00.php on line 8 - -1 - -1 xxx int(4) string(14) "Resource id #4" string(14) "Resource id #4" string(14) "Resource id #4" Actual result: -------------- -1 -1 - -1 xxx int(4) string(14) "Resource id #4" string(14) "Resource id #4" string(14) "Resource id #4" ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=39764&edit=1

« previous php.bugs (#106059) next »