php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51795 Loose comparison too loose
Submitted: 2010-05-11 16:25 UTC Modified: 2010-05-12 01:27 UTC
From: slevinski at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.13 OS: Ubuntu
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: slevinski at gmail dot com
New email:
PHP Version: OS:

 

 [2010-05-11 16:25 UTC] slevinski at gmail dot com
Description:
------------
if ("0080" == "80e0") echo "Smoking crack!";

I created a double octet coded character set.  I'm using 4 hex to represent each character.  I'm using a switch statement to get a token based on the value.  The string "80e0" is incorrectly setting off the case "0080" statement.

The strict comparison of === works as expected, but the switch uses a loose comparison.  

Test script:
---------------
if ("0080" == "80e0") {
  echo "Too loose";
} else {
  echo "Just right";
}

Expected result:
----------------
Just right

Actual result:
--------------
Too loose

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-11 22:18 UTC] dtajchreber@php.net
-Status: Open +Status: Bogus
 [2010-05-11 22:18 UTC] dtajchreber@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

"0080" turns into an 80 and is stored as an int. "80e0" turns into 80 x 10^0 which is also 80 but is stored as a float. 80(int) == 80(float). 80(int) !== 80(float). I didn't understand the rest of what you wrote but php.net/hexdec might be of some use. 

See: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
 [2010-05-12 01:27 UTC] rasmus@php.net
Or prefix your strings with 0x if you want to force hex.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri May 29 18:01:25 2020 UTC