note 24925 deleted from language.operators.comparison by sniper

From: Date: Mon, 13 Oct 2003 00:17:02 +0000
Subject: note 24925 deleted from language.operators.comparison by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58435@lists.php.net to get a copy of this message
Note Submitter: tlittle@deepdesign.com.au ---- (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 "===".

« previous php.notes (#58435) next »