note 27568 added to language.operators.comparison
| From: | AJCartmell at ricardo dot com | Date: | Wed, 11 Dec 2002 10:16:35 +0000 |
| Subject: | note 27568 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40755@lists.php.net to get a copy of this message | ||
To clarify on 'numerical' string equality comparisons, for example when testing progam
version numbers where "two.three" is not the same as "two.thirty":
$a = "2.3";
$b = "2.30";
$a == $b => true (unexpected)
(string)$a == (string)$b => true (unexpected)
To fix this problem append a space to the ends of each string, or use the === operator (which
doesn't have the implicit casting to numbers that the == operator does)
"$a " == "$b " => false
$a.' ' == $b.' ' => false
$a === $b => false
The first is the slowest, the second 10% faster, the last seven times faster than the second.
--
http://www.php.net/manual/en/language.operators.comparison.php
http://master.php.net/manage/user-notes.php?action=edit+27568
http://master.php.net/manage/user-notes.php?action=delete+27568
http://master.php.net/manage/user-notes.php?action=reject+27568