strcmp equivelant for integers (intcmp) in PHP

前端 未结 8 2126
故里飘歌
故里飘歌 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:24

    You could use

    function intcmp($a,$b)
        {
        return ($a-$b) ? ($a-$b)/abs($a-$b) : 0;
        }
    

    Although I don't see the point in using this function at all

    0 讨论(0)
  • 2020-12-02 20:29

    I wouldn't call it dirty per se, it seems valid enough. But I can't think where I would use that function. My only suggestion might be to include else:

    function intcmp($a,$b)
    {
        if((int)$a == (int)$b)return 0;
        else if((int)$a  > (int)$b)return 1;
        else if((int)$a  < (int)$b)return -1;
    }
    
    0 讨论(0)
提交回复
热议问题