PHP: Why do we need string comparison function?

2020-07-06 08:25发布

The comparision operators < <= > >= can be applied for strings as well. So why do we need special function for string comparision: strcmp ?

2条回答
萌系小妹纸
2楼-- · 2020-07-06 08:54

Because there are several variations:

Depending on the function, the answer to these questions vary:

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

查看更多
够拽才男人
3楼-- · 2020-07-06 09:07

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.

查看更多
登录 后发表回答