Bug #61140 [Opn->Nab]: foreach such array('str' => 'str', 0 => 0), compare key and 'str' error
Edit report at https://bugs.php.net/bug.php?id=61140&edit=1
ID: 61140
Updated by: rasmus@php.net
Reported by: imlivv at gmail dot com
Summary: foreach such array('str' => 'str', 0 => 0), compare
key and 'str' error
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: windows and *nix
PHP Version: 5.3SVN-2012-02-20 (SVN)
Block user comment: N
Private report: N
New Comment:
Use === to do a strict comparison without type juggling. More info here:
http://www.php.net/manual/en/language.operators.comparison.php
Previous Comments:
------------------------------------------------------------------------
[2012-02-20 10:06:12] imlivv at gmail dot com
Description:
------------
iterate an array which included both int(0) and string type key with foreach, and
compare key and string value, get an error result!
Test script:
---------------
$test = array(
'str' => 'str',
0 => 0,
1 => 1
);
foreach ($test as $k=>$v) {
if ('str' == $k)
echo "$k == str \n";
}
Expected result:
----------------
output 'str == str ' only
Actual result:
--------------
output:
str == str
0 == str
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61140&edit=1
Thread (2 messages)