Bug #64324 [Nab]: Why 0 == 'BOOK' ?
| From: | rasmus@php.net | Date: | Sat, 02 Mar 2013 01:40:32 +0000 |
| Subject: | Bug #64324 [Nab]: Why 0 == 'BOOK' ? | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-177511@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: Not a bug
Type: Bug
Package: *General Issues
Operating System: all
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
No it doesn't make more sense. This would make 12=='TEST'
You cast to a type in the comparison, not to some third type. Besides changing
this would break millions of lines of code. Not going to happen.
Previous Comments:
------------------------------------------------------------------------
[2013-03-02 01:19:42] dosergio at ig dot com dot br
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'.
------------------------------------------------------------------------
[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!";
------------------------------------------------------------------------
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