Re: strcmp() versus ==
| From: | Shawn McKenzie | Date: | Mon, 16 Mar 2009 22:06:35 +0000 |
| Subject: | Re: strcmp() versus == | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-290138@lists.php.net to get a copy of this message | ||
Shawn McKenzie wrote:
> Paul M Foster wrote:
>> I had never completely read over the rules with regard to comparisons in
>> PHP, and was recently alarmed to find that $str1 == $str2 might not
>> compare the way I thought they would. Is it common practice among PHP
>> coders to use strcmp() instead of == in making string comparisons? Or am
>> I missing/misreading something?
>>
>> Paul
>>
>
> I would use $str1 === $str2 if you want to make sure they are both
> strings and both the same value.
>
> Since PHP is loosely typed, "0" == 0 is true however "0" === 0 is false.
>
If you want to force a string comparison you can also use:
(string)$str1 == (string)$str2
--
Thanks!
-Shawn
http://www.spidean.com