strcmp equivelant for integers (intcmp) in PHP

前端 未结 8 2166
故里飘歌
故里飘歌 2020-12-02 19:27

So we got this function in PHP

strcmp(string $1,string $2) // returns -1,0, or 1;

We Do not however, have an intcmp(); So i created one:

8条回答
  •  渐次进展
    2020-12-02 20:21

    Does it have to be +1 and -1? If not, just return (int) $a - (int) $b. I don't like the divide that someone else recommended, and there's no need to check for all three cases. If it's not greater and not equal, it must be less than.

    return (int) $a > (int) $b ? 1 : (int) $a == (int) $b ? 0 : -1;
    

提交回复
热议问题