strcmp equivelant for integers (intcmp) in PHP

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

    why reinventing the wheel? http://php.net/manual/en/function.strnatcmp.php

    echo strnatcmp(1, 2) . PHP_EOL; // -1
    echo strnatcmp(10, 2) . PHP_EOL; // 1
    echo strnatcmp(10.5, 2) . PHP_EOL; // 1 - work with float numbers
    echo strnatcmp(1, -2) . PHP_EOL; // 1 - work with negative numbers
    

    Test it here: https://3v4l.org/pSANR

提交回复
热议问题