note 14462 deleted from language.operators.comparison by sniper

From: Date: Mon, 13 Oct 2003 00:14:36 +0000
Subject: note 14462 deleted from language.operators.comparison by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58419@lists.php.net to get a copy of this message
Note Submitter: ---- when comparing expressions with different datatypes, remember that if any expression is of numeric or boolean datatype, both expressions will be type-casted to a numeric before the comparison is performed. Because expressions could hold floating points, so that numéric values retain their full precision (as if strings actually contained "double" datatyped values). So: false == '' evaluates as: double(false) == double(''), i.e. as: 0.0 == 0.0, and the result is true. And: false == '0.0' is also true. And: 0 == '0.0' is true. And: 0.0 == '0' is also true. But: false == '0.1' is false, because numeric values 0.0 and 0.1 are compared. When evaluating '0' == '' the result is false because (here only), no implicit typecast of strings to numeric is performed, so strings are actually compared as strings. When both members are not strings but boolean or numeric, PHP implicitly converts both members to the same datatype with the maximum precision (this does not necessarily implies a comparison of double but could imply a comparison of integers, but this is just a run-time optimization, and does not change the result). The same applies to other compare operators where one member is a string. So you should assume that compare operators can perform on both strings, else they will implicitly perform comparison between floating-point values with an implicit datatype conversion. Note that the implicit conversion of string to numeric is locale dependant on systems that correctly implement POSIX and C99 locales: with the C or US English locale, '0.1'==0 is false and '0,1'==0 is true, but in the French locale the results are exactly reversed!!! (In the French locale, the decimal-point separator is not the dot but the comma, so '0.1' evaluates as number 0.0, and '0,1' evaluates as number 0.1).

« previous php.notes (#58419) next »