php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64376 Comparison does not return correct value
Submitted: 2013-03-07 12:08 UTC Modified: 2013-03-08 16:55 UTC
From: ludko2 at gmail dot com Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 5.4.12 OS: win7 64bit
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: ludko2 at gmail dot com
New email:
PHP Version: OS:

Further comment on this bug is unnecessary.

 

 [2013-03-07 12:08 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-07 15:54 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2013-03-07 15:54 UTC] 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 20:32 UTC] 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 21:43 UTC] 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-08 09:12 UTC] 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-08 16:55 UTC] rasmus@php.net
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.
 [2013-03-08 16:55 UTC] rasmus@php.net
-Block user comment: No +Block user comment: Yes
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 04:01:27 2024 UTC