#43839 [Opn->Bgs]: Cast problem with the equal operator (==)
| From: | colder@php.net | Date: | Mon, 14 Jan 2008 15:41:59 +0000 |
| Subject: | #43839 [Opn->Bgs]: Cast problem with the equal operator (==) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-121266@lists.php.net to get a copy of this message | ||
ID: 43839
Updated by: colder@php.net
Reported By: technique at ircf dot fr
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Linux Ubuntu
PHP Version: 5.2CVS-2008-01-14 (snap)
New Comment:
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 strings are understood as numeric values. This is not exactly
an int cast.
i.e. "01a" == "001a" is not true, as the operands are not numeric.
however (int)"01a" == (int)"001a" == 1
Previous Comments:
------------------------------------------------------------------------
[2008-01-14 13:33:25] technique at ircf dot fr
Description:
------------
I have noticed that the equal operator will systematically try to cast
expressions as integers, even when it is useless.
This may cause problems like this (see the code below).
Wouldn't be better to cast only one operand ONLY if the two operands
are not of the same type ? For example :
"1" == 1 , would cast the right operand (1) as a string and then
compare it with the first
"123" == "456", would not cast any operand because they already have
the same type
I know that this issue can be solved using the identity operator (===)
but I find weird that "001" == "0001" returns true, or am I missing
something ?
Reproduce code:
---------------
<?php
if ("0001" == "000001"){
echo "Impossible !!!";
}
?>
Expected result:
----------------
Nothing
Actual result:
--------------
Impossible !!!
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=43839&edit=1