Bug #64324 [Nab]: Why 0 == 'BOOK' ?
| From: | dosergio at ig dot com dot br | Date: | Sat, 02 Mar 2013 01:19:42 +0000 |
| Subject: | Bug #64324 [Nab]: Why 0 == 'BOOK' ? | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-177510@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
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:
A good rule to be implemented by PHP is:
If a comparison of simple equality ( == ) or simple inequality ( != ) is done between two different
data types, PHP should cast both to boolean before comparing.
Because 'TEXT' casts to true, 0 casts to false so 0 != 'TEXT' makes more sense
than 0 == 'TEXT'.
Previous Comments:
------------------------------------------------------------------------
[2013-02-28 19:12:25] dosergio at ig dot com dot br
OK, you are right. That was the explanation I wanted: it depends on the type you compare.
if( false == 'TEST') works correctly.
Now it makes a little more sense to me.
But javascript is still superior because inside a if() I suspect that any language should try to
cast both to boolean.
------------------------------------------------------------------------
[2013-02-28 19:04:24] rasmus@php.net
We don't want a special case for 0. By your logic 12 == 'TEST' should be true.
You are assuming a cast to boolean even though neither side of the comparison is
a boolean. Note that true == 'TEST' will match because here we cast to boolean.
But 'TEST' cast to an integer is going to give you 0.
------------------------------------------------------------------------
[2013-02-28 18:58:51] dosergio at ig dot com dot br
Conclusion:
The exact analysis above done in javascript says I am right.
I have no doubt that javascript makes more use of logic that php.
------------------------------------------------------------------------
[2013-02-28 18:51:56] dosergio at ig dot com dot br
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!";
------------------------------------------------------------------------
[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 ===
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=64324
--
Edit this bug report at https://bugs.php.net/bug.php?id=64324&edit=1