strcmp equivelant for integers (intcmp) in PHP

前端 未结 8 2186
故里飘歌
故里飘歌 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条回答
  •  旧时难觅i
    2020-12-02 20:16

    Purely as some additional information, there has been an accepted RFC for this (https://wiki.php.net/rfc/combined-comparison-operator).

    So, the comparison function would be along the lines of ...

     $right; });
    ?>
    

    A few really nice feature here is that the comparison is done in exactly the same way as all other comparisons. So type juggling will happen as expected.

    As yet, there is no magic __forCompare() like method to allow an object to expose a comparison value. The current proposal (a different RFC) is to have each object be injected into every other object during the comparison so that it does the comparison - something which just seems odd to me - potential opportunity for recursion and stack overflow ... ! I would have thought either injecting the type of object for comparison (allowing an object the ability to represent appropriate values depending upon the type of comparison) or a blind request for a value that the object can serve up for comparison, would have been a safer solution.

    Not yet integrated into PHP-NG (PHP 7 at the moment), but hopefully will be soon.

提交回复
热议问题