php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61140 foreach such array('str' => 'str', 0 => 0), compare key and 'str' error
Submitted: 2012-02-20 10:06 UTC Modified: 2012-02-20 10:44 UTC
From: imlivv at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3SVN-2012-02-20 (SVN) OS: windows and *nix
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: imlivv at gmail dot com
New email:
PHP Version: OS:

 

 [2012-02-20 10:06 UTC] 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 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-20 10:44 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-02-20 10:44 UTC] rasmus@php.net
Use === to do a strict comparison without type juggling. More info here:

http://www.php.net/manual/en/language.operators.comparison.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 11:01:32 2025 UTC