note 43754 added to language.operators.comparison
| From: | jeronimo at DELETE_THIS dot transartmedia dot com | Date: | Fri, 02 Jul 2004 08:01:12 +0000 |
| Subject: | note 43754 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72310@lists.php.net to get a copy of this message | ||
For converted Perl programmers: use strict comparison operators (===, !==) in place of string
comparison operators (eq, ne). Don't use the simple equality operators (==, !=), because ($a ==
$b) will return TRUE in many situations where ($a eq $b) would return FALSE.
For instance...
"mary" == "fred" is FALSE, but
"+010" == "10.0" is TRUE (!)
In the following examples, none of the strings being compared are identical, but because PHP *can*
evaluate them as numbers, it does so, and therefore finds them equal...
<?
echo ("007" == "7" ? "EQUAL" : "not equal");
// Prints: EQUAL
// Surrounding the strings with single quotes (') instead of double
// quotes (") to ensure the contents aren't evaluated, and forcing
// string types has no effect.
echo ( (string)'0001' == (string)'+1.' ? "EQUAL" : "not
equal");
// Prints: EQUAL
// Including non-digit characters (like leading spaces, "e", the plus
// or minus sign, period, ...) can still result in this behavior, if
// a string happens to be valid scientific notation.
echo (' 131e-2' == '001.3100' ? "EQUAL" : "not equal");
// Prints: EQUAL
?>
If you're comparing passwords (or anything else for which "near" precision isn't
good enough) this confusion could be detrimental. Stick with strict comparisons...
<?
// Same examples as above, using === instead of ==
echo ("007" === "7" ? "EQUAL" : "not equal");
// Prints: not equal
echo ( (string)'0001' === (string)'+1.' ? "EQUAL" : "not
equal");
// Prints: not equal
echo (' 131e-2' === '001.3100' ? "EQUAL" : "not equal");
// Prints: not equal
?>
----
Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43754
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43754&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43754&report=yes
Search -- http://master.php.net/manage/user-notes.php