Edit report at https://bugs.php.net/bug.php?id=64324&edit=1
ID: 64324
User updated by: dosergio at ig dot com dot br
Reported by: dosergio at ig dot com dot br
Summary: Why 0 == 'BOOK' ?
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: all
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I know the use of ===. The supposed 'bug' that I think exists, is comparing 0 to a
non-empty String with double ==
My concern is because:
Observation 1: null, 0, "0", and "" all result as FALSE.
Observation 2: BUT... "A STRING" evaluates as TRUE.
SO...
0 == "" makes sense
BUT...
0 == "A NON-EMPTY STRING" makes no sense. IMHO False would be the right answer.
Take a little time to examine this:
if( 0 ) echo "0 works as TRUE<br>"; else echo "0 works as false
<br>";
if( "TEST") echo "'TEST' works as TRUE<br>"; else echo
"'TEST' works as false <br>";
if( 0 == 'TEST') echo "But 0 == 'TEST' belies the statements above!";
Previous Comments:
------------------------------------------------------------------------
[2013-02-28 15:22:59] rasmus@php.net
You are doing a loose comparison between two different types. PHP has to pick a
type for it. In this case it does 0 == (int)'TEST' and casting 'TEST' to an int
is obviously going to give you 0. This is what you are going to want in most
cases. eg. 12 == "12 " (with an extra space there). Chances are the "12 " came
from user input since everything that comes from either the browser or your
backend database comes to you as strings, you are going to want that comparison
to work. If you cast both to strings instead they wouldn't.
If you don't want PHP to guess, use ===
------------------------------------------------------------------------
[2013-02-28 14:55:44] dosergio at ig dot com dot br
*If the comparison WERE
------------------------------------------------------------------------
[2013-02-28 14:54:24] dosergio at ig dot com dot br
Description:
------------
If the comparison where
0 == '' that would make sense.
But I am chanlenging some PHP expert to convince us that the result 1 is right for the comparison 0
== 'ANY STRING'
Test script:
---------------
if( 0 == 'TEST'){
echo "PHP thinks 0 is equal 'TEST'";
}
Expected result:
----------------
0 is not similar to a string that is NOT empty.
this comparison should return FALSE.
Actual result:
--------------
it returns 1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64324&edit=1