note 24925 added to language.operators.comparison

From: Date: Thu, 05 Sep 2002 06:12:12 +0000
Subject: note 24925 added to language.operators.comparison
Groups: php.notes 
Request: Send a blank email to php-notes+get-36147@lists.php.net to get a copy of this message
(PHP version 4.1.2) Not only is ("001" == "00001") true, but so is the expression ((string)"1.0e34" == (string)"1E34"). It appears that if both strings can be interpreted as double-precision numbers in a comparison, they will, even if you explicitly cast them otherwise. Furthermore, the rules PHP uses are wrong: var_dump("0" == "1e-323"); // false: 1e-323 represents a non-zero double-precision number var_dump("0" == "10e-324"); // false: as above var_dump("0" == "1e-324"); // true: "1e-324" is interpreted as double-precision, but underflows to 0 var_dump("0" == "10e-325"); // true: as above var_dump("0" == "1e-325"); // false: "1e-325" is too small to be interpreted as numeric var_dump("1" == "1e-323"); // false: 1 != 1e-323 var_dump("1" == "1e-324"); // false: 1 != 0 (underflow) var_dump("1" == "1e-325"); // false: too small to be numeric, and "1" != "1e-325" var_dump(1 == "1e-323"); // false (as above) var_dump(1 == "1e-324"); // false (as above) var_dump(1 == "1e-325"); // true: 1e-325 is too small to be implictly cast to double, but the other operand is numeric so it must be cast to integer 1 (followed by non-numeric rubbish) var_dump(1 == (double)"1e-325"); // false: "1e-325" is explicitly cast to double, underflowing to 0 Moral of the story: Never use "==" comparisons between strings that may possibly contain numbers. Always use "===". -- http://www.php.net/manual/en/language.operators.comparison.php http://master.php.net/manage/user-notes.php?action=edit+24925 http://master.php.net/manage/user-notes.php?action=delete+24925 http://master.php.net/manage/user-notes.php?action=reject+24925

« previous php.notes (#36147) next »