php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64324 Why 0 == 'BOOK' ?
Submitted: 2013-02-28 14:54 UTC Modified: 2013-03-02 02:07 UTC
From: dosergio at ig dot com dot br Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dosergio at ig dot com dot br
New email:
PHP Version: OS:

 

 [2013-02-28 14:54 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-28 14:55 UTC] dosergio at ig dot com dot br
*If the comparison WERE
 [2013-02-28 15:22 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2013-02-28 15:22 UTC] 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 18:51 UTC] 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 18:58 UTC] 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 19:04 UTC] 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 19:12 UTC] 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-03-02 01:19 UTC] 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-03-02 01:40 UTC] rasmus@php.net
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.
 [2013-03-02 01:46 UTC] dosergio at ig dot com dot br
12 == 'TEXT' in the "boolean" point of view is correct.
Javascript agrees with it.
 [2013-03-02 01:49 UTC] rasmus@php.net
So by your logic 12=='13'
Do you have any idea how much code that would break?
The web is not typed. Everything comes across as strings. And everyone does stuff 
like if($_GET['age']==19) { ... }
which you are proposing to break.
Same goes for data retrieved from databases. Everything comes back as strings.
So no, this is simply not going to happen. Please stop.
 [2013-03-02 02:07 UTC] dosergio at ig dot com dot br
There is a solution.

See that all examples I gave were made using NON-NUMERIC texts. ('BOOK', 'TEXT', ETC) You are right thinking about numeric strings like '234'.

If text is numeric, php could continue treating them as numbers - that would be an exception to the rule.

BUT... if the text is NOT numeric (\D+) then it could be casted to boolean.
'12' '13' '1987' would NOT be casted to boolean.
'car' 'soap' '#ffcc00' would.

"If comparing a NON-NUMERIC text ex: "book" to a number, both will be casted to boolean before comparing".

That would solve the problem, I think.

Remember that
if(0) results false
if('BOOK') results true
so...  0 == 'BOOK' breaks the logic. Think about with calm. I will stop now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 13:01:31 2024 UTC