Bug #64376 [Nab]: Comparison does not return correct value
| From: | rasmus@php.net | Date: | Thu, 07 Mar 2013 21:43:50 +0000 |
| Subject: | Bug #64376 [Nab]: Comparison does not return correct value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-177658@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64376&edit=1
ID: 64376
Updated by: rasmus@php.net
Reported by: ludko2 at gmail dot com
Summary: Comparison does not return correct value
Status: Not a bug
Type: Bug
Package: *Programming Data Structures
Operating System: win7 64bit
PHP Version: 5.4.12
Block user comment: N
Private report: N
New Comment:
Yes, numeric strings are intinally treated as integers for array keys to avoid
the confusion you would have if you had: [0=>1, '0'=>2]
If you want to compare your array keys as strings, there are plenty of ways to
do that. eg.
(string)$key == "c"
strcmp($key, "c")
"$key" == "c"
Previous Comments:
------------------------------------------------------------------------
[2013-03-07 20:32:03] ludko2 at gmail dot com
I object.. I am not comparing the string to int!
I am comparing an array key (that may be "0") to a string!
If this is not a bug, than there is a bug, that array key is transformed to an int if it is a
string!
------------------------------------------------------------------------
[2013-03-07 15:54:19] rasmus@php.net
You are comparing a string to an int. Both sides are cast to int. (int)"b0123..."
cast to an integer is 0. 0==0.
Use === to force a strict comparison, or explicitly cast your operands to your
desired type.
------------------------------------------------------------------------
[2013-03-07 12:08:45] ludko2 at gmail dot com
Description:
------------
Comparison between string and zero and string returns true?!?
var_dump("b0180e6574921b8b8de3c40bfd046571"==0);
tested on 5.4.6 win 7 64bit
Test script:
---------------
$arr = array("0"=>"b");
var_dump($arr);//array(1) { [0]=> string(1) "b"}
foreach($arr as $k=>$v){
var_dump($k == "c"); // returns true (FAIL)
}
var_dump("b"==0);//returns true (FAIL)
var_dump("a"==1); // returns false (OK)
Expected result:
----------------
var_dump("b"==0);// expecting false
Actual result:
--------------
var_dump("b"==0);// returning true
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64376&edit=1