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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 22:01:33 2024 UTC