php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51781 array index 0 problem
Submitted: 2010-05-10 09:35 UTC Modified: 2010-05-10 17:42 UTC
From: feng_evan at yahoo dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.13 OS: windows xp
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: feng_evan at yahoo dot com
New email:
PHP Version: OS:

 

 [2010-05-10 09:35 UTC] feng_evan at yahoo dot com
Description:
------------
Array index value judgments must use absolute equal?

Test script:
---------------
$arr = array();
$arr['0'] = 'zero';
$arr['keyword'] = 'keyword';
foreach($arr as $key => $val) {
    if ($key == 'keyword') {
    //if ($key === 'keyword') { //ok
        echo $val."\n";
    }
}
//output:zero keyword


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-10 10:57 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-05-10 10:57 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

numeric indexes are stored as integers, comparing integer 0 with a string not starting with a digit gives true as the string is casted to an integer.

Expected, documented, behavior. Won't change.
 [2010-05-10 17:32 UTC] feng_evan at yahoo dot com
yes,i can see. "If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement. "

but,unacceptable intuitively
 [2010-05-10 17:42 UTC] johannes@php.net
Well people expect that this will work:

$list = array('foo', 'bar', 'baz');
$value = $list[$_GET['id']];

And well, the behavior is like that >10 years even if it might be considered bad breaking it is worse.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Jun 03 06:01:30 2020 UTC