strcmp equivelant for integers (intcmp) in PHP

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

    At a glance, yes it feels dirty. Except there must be a good reason you wrote that instead of just using the actual ==, >, and < operators. What was the motivation for creating this function?

    If it were me, I'd probably just do something like:

    $x = $a==$b ? 0 : ($a>$b ? 1 : ($a<$b ? -1 : null));
    

    I realize this is just as ugly, and the : null; - not sure if PHP requires it or if I could have just done :; but I don't like it and that code should never execute anyway... I think I'd be a lot less confused about this if I knew the original requirements!

提交回复
热议问题