The comparision operators < <= > >= can be applied for strings as well. So why do we need special function for string comparision: strcmp ?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Because there are several variations:
Depending on the function, the answer to these questions vary:
- Is it case sensitive? (
strcmpvsstrcasecmp,strnatcmpvsstrnatcasecmp) - Depends it depend on the locale? (
strcolldoes) - Can I specify a collation? (
strcollis affected bysetlocale)
Additionaly, the comparison operators also give true or false. strcmp gives an integer so it can encode simultaneously whether there's identity (return 0) or, if it not, which is is bigger (depending on whether the value is positive or negative).
回答2:
Although there are no overloads in PHP for strcmp, strcmp results in 3 different values -1 for less than, 0 for equals and +1 for greater than the compared string. With < = <= > >= you will have (sometimes) to do multiple checks one after another.