Re: PHP 4.0 Bug #9186 Updated: string compare with "==" does not work correctly
| From: | Zak Greant | Date: | Fri, 09 Feb 2001 19:31:24 +0000 |
| Subject: | Re: PHP 4.0 Bug #9186 Updated: string compare with "==" does not work correctly | ||
| References: | 1 2 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-46104@lists.php.net to get a copy of this message | ||
How is this? I modified what Zeev said a wee bit:
== is a 'loose' comparison operator [1].
In most cases, this is more convenient - the operator does the right thing
without needing any coaxing. In some cases, things will happen that are
confusing - particularly when comparing values that have leading zeros (0)
or when comparing very long numbers that are inside a string.
The basic rule to follow is this:
Character-by-character comparison is performed only if both arguments are
strings and at least one of the strings does not look like a number [2].
Another way to state this rule is:
- If at least one of the arguments is not a string, a numeric comparison is
made.
- If both arguments are strings, but both of them look like numbers, a
numeric comparison is made.
- If both arguments are strings (and at least one of them doesn't look like
a number), a string comparison is made.
[1] If you need to perform an exact comparison between two values, take a
look at the 'strict' comparison operator (===) and the strcmp() function.
[2] See the manual section on types and strings to learn what rules PHP
follows for recognising numbers within strings.
(Thanks to Zeev for clarifying this issue.)
--zak