On Tue, 2009-03-17 at 08:09 +0200, Alpár Török wrote: > 2009/3/17 Shawn McKenzie <nos...@mckenzies.net>: > > 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 > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > AFAIK strcmp and strncmp are faster. At least for the second i > remember seeing benchmarks that proved that.
Then you must have seen a bad benchmark. strncmp() has specific purposes and so does strcmp() where you would choose it instead of == or ===. But with respect to speed, if all you want to know is whether two strings are the same then == or === will be faster since they are operators and do not have the same overhead as a function call. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php