PHP:为什么需要字符串比较函数?

2022-01-25 00:00:00 string compare php

比较运算符 <<= >>= 也可以应用于字符串.那么为什么我们需要特殊的字符串比较函数:strcmp?

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

推荐答案

因为有几种变体:

根据功能,这些问题的答案各不相同:

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

  • 区分大小写吗?(strcmp vs strcasecmp, strnatcmp vs strnatcasecmp)
  • 取决于语言环境?(strcoll 会)
  • 我可以指定排序规则吗?(strcollsetlocale 影响)

此外,比较运算符还给出 truefalse.strcmp 给出一个整数,因此它可以同时编码是否存在身份(返回 0),或者如果没有,则更大(取决于值是正数还是负数).

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

相关文章