Jump to content

Uses of the strcmp function?


extrovertive

Recommended Posts

i found this comment in the strcmp section of the manual, which seems to be a good explanation:

[quote]
One thing to note in comparison with ==

When we make a comparison with == php automaticly converts strings to integers when either side of the comparison is an integer, f.e.:
<?
$value = 0;
if($value == "submit") {
  echo "Let's submit";
}
?>
Above would be succesful, since "submit" is converted to an integer (eq 0) and the equation is would return true; (that's why (1 == "1submit") would also return true)

That's why we should use strcmp or === (checks type also), for string comparisons.

So my conclusion is that when comparing string, you'd better not make use of == (use strmp or === instead). For integer comparisons the == equation can be usefull, since our values will always be casted to an integer (1 == "1" returns true).
[/quote]
Link to comment
Share on other sites

because it's a binary comparison.  it breaks the strings down to their binary equivelants and compares them.  Therefore it needs 3 returnable values: less than, equal or greater than.

[code]
strcmp(101,100);    //returns 1 because 101 is greater than 100
strcmp(101,101);    //returns 0 because they are equal
strcmp(1010,1011); //returns -1 becuase 1010 is less than 1011
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.