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

From: Date: Thu, 07 Dec 2006 10:50:55 +0000
Subject: #39764 [Opn->Fbk]: array_key_exists inconsistent behavior
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-106054@lists.php.net to get a copy of this message
ID: 39764 Updated by: tony2001@php.net Reported By: techtonik@php.net -Status: Open +Status: Feedback -Bug Type: Scripting Engine problem +Bug Type: Arrays related Operating System: Windows2000 PHP Version: 4.4.4 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [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 (#106054) next »