Re: comparing a string
| From: | Rafael | Date: | Wed, 21 Jun 2006 00:30:30 +0000 |
| Subject: | Re: comparing a string | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-238442@lists.php.net to get a copy of this message | ||
(inline)
Adam Zey wrote:
Rafael wrote:You shouldn't rely on what other languages do, but the one you're working with; PHP has no explicit data-types and manages strings as PERL does. Just as you say there's a reason why "==" supports strings, there's also a reason (or more) for strcmp() to exists --it's just safer to use strcmp() instead of "==", e.g: 24 == "24/7"This is PHP, not C. Operators such as == support strings for a reason, people should use them as such.A single "=" it's an assignment, not a comparison; and though it sometimes work, you shouldn't compare strings with "==", but using string functions, such as strcmp()... or similar_text(), etc.
If you need to ensure type, (so that 0 == "foo" doesn't return true), then you can use ===. Using a function call that does more than you need when there is an operator to achieve the same goal is bad advice.I think you haven't encounter a "special case" to make you understand "==" does NOT have the same behaviour as strcmp() It's just like the (stranger) case of the loop for ( $c = 'a'; $c <= 'z'; $c ++ )
echo $c;
Not to mention the fact that it leads to harder to read code. Which of these has a more readily apparent meaning? if ( strcmp($foo,$bar) == 0 ) if ( $foo === $bar )That might be true, either way you need to know the language to understand what the first line does and that the second isn't a typo. -- Atentamente / Sincerely, J. Rafael Salazar Magaña