Jump to content


Photo

Uses of the strcmp function?


  • Please log in to reply
3 replies to this topic

#1 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 14 October 2006 - 06:56 AM

I know strcmp returns 0 if both string are the same -1 is string a is < b 1 is a > b.

But what are some practical uses of it in an application?

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 October 2006 - 07:04 AM

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

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).


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 14 October 2006 - 09:15 AM

Ah, I see. I've been using if($data == 'mystring') the whole time; should've use === instead.

But, if the string is the same, why return 0? What's the -1 and 1 for in the comparison?

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 October 2006 - 05:15 PM

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.

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

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users