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:

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.


Previous Comments:
------------------------------------------------------------------------
[2013-03-02 01:49:56] ras...@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 01:46:02] 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:40:32] ras...@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: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.

------------------------------------------------------------------------


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

Reply via email to