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.

Bookmark and Share

Somewhat related posts

2 Responses to “strcmp”

  1. James Says:

    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.

Leave a Reply