Bug #64376 [Nab]: Comparison does not return correct value
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: No
+Block user comment: Yes
Private report: N
New Comment:
Nope, sorry, this will not change. It would break millions of lines of existing
code. You just need to learn how loose comparisons work in PHP.
Previous Comments:
------------------------------------------------------------------------
[2013-03-08 09:12:25] ludko2 at gmail dot com
i am sorry to be that bold, but i believe it is a bug in PHP..
comparision should return the true if values are equal.. how is it that zero number is equal to any
string? it is against common sense, and i believe it should be fixed..
i have revealed this in our information system that uses key zero in the array.. i believe this is
used not only in our information system, and should be fixed in PHP distribution not to cause
undesired bugs
Could you please change the state of this bug from "not a bug" to "bug"?
I believe noone wants intentionsly comparing zero to string to return true, so if you repair it, you
should not affect any information systems already in place.
------------------------------------------------------------------------
[2013-03-07 21:43:50] rasmus@php.net
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"
------------------------------------------------------------------------
[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
Thread (6 messages)