#39764 [Opn]: array_key_exists inconsistent behavior
| From: | techtonik@php.net | Date: | Thu, 07 Dec 2006 10:41:54 +0000 |
| Subject: | #39764 [Opn]: array_key_exists inconsistent behavior | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-106053@lists.php.net to get a copy of this message | ||
ID: 39764
User updated by: techtonik@php.net
Reported By: techtonik@php.net
Status: Open
Bug Type: Scripting Engine problem
Operating System: Windows2000
PHP Version: 4.4.4
New Comment:
"Expected result" and "Actual result" should be switched. Sorry for
confusion.
Previous Comments:
------------------------------------------------------------------------
[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