strcmp
strcmp() compares two strings and gives -1, 0 or 1 depending on wheather the first string is "smaller", equal to or greater than the second one.
< ?php
echo strcmp('aa','bb'); // prints -1
echo strcmp('bb','bb'); // prints 0
echo strcmp('zz','yy'); // prints 1
?>
Thus being said there's an error in the php certification guide that explains the strcasecmp() function, which is the same as strcmp() only case insensitive. There is a snippet in the book that goes like:
< ?php
$a = 'hello';
if (strcasecmp($a, 'HELLO')) {
echo 'the same';
}?>
The book says that the echo will be executed but it will not, as the comparison will return 0, since 'hello' and 'HELLO' are the same string when regarded at case-insensitively.
This entry was posted on Tuesday, March 29th, 2005 and is filed under PHP certification. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

January 6th, 2006 at 8:02 am
just adding on there… 0 is equivalant to boolean false. so the overal if statement won’t be true. if you where to do
[code]
[/code]
then it would execute.
– Sorry – i really like your blog but i felt you didn’t mention that.
November 1st, 2010 at 4:55 pm
That’s pretty much an artifact of C which made its way into PHP. Since C strings were just character arrays or pointers to char arrays, == compared the value at the first address, not the value of the string itself.
As James probably tried to say but it seems to have gotten eaten by the comment parser… if (!strcasecmp($str1, $str2)) {/* they’re equal */}
One char typo in the book, but a pretty major one since the result is exactly the opposite of what the author says it is