|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 10:00:01 2025 UTC |
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.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.