Bug #64324 [Opn->Nab]: Why 0 == 'BOOK' ?
| From: | rasmus@php.net | Date: | Thu, 28 Feb 2013 15:22:59 +0000 |
| Subject: | Bug #64324 [Opn->Nab]: Why 0 == 'BOOK' ? | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-177462@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64324&edit=1
ID: 64324
Updated by: rasmus@php.net
Reported by: dosergio at ig dot com dot br
Summary: Why 0 == 'BOOK' ?
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: all
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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 ===
Previous Comments:
------------------------------------------------------------------------
[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