On Mon, Dec 2, 2024 at 1:31 PM Rowan Tommins [IMSoP]
<imsop....@rwec.co.uk> wrote:
>
> On 01/12/2024 03:41, mickmackusa wrote:
>
> I can appreciate that.  Going forward, is there any benefit to preserving the 
> behavior of returning integers beyond -1, 0, and 1?
> Should these topically related functions receive a new last argument? bool 
> $distance = false
>
> The functions are not attempting to return a meaningful "distance", this is 
> just an optimisation: the intended use case is as a callback to functions 
> like usort() which only care about <0, 0, >0, so no CPU time is spent 
> normalising the result to specific values.
>
> The documentation is simply mistaken in saying "-1" instead of "a value less 
> than zero" and "1" instead of "a value more than zero".

This is exactly it. This was already fixed in the upgrading guide a while ago:

https://github.com/php/doc-en/pull/3450

> The strcmp, ... functions, using binary safe string comparison is no longer
> guaranteed to return strlen($string1) - strlen($string2) when string lengths 
> are
> not equal, but may now return -1 or 1 instead. Instead of depending on any
> concrete value, the return value should be compared to 0.

The reason for this change is not to unify the output, but rather to
avoid a bug on platforms where sizeof(size_t) > sizeof(int). When the
length of the string exceeds what int can hold, the difference in
string sizes can potentially not be represented by int. The data
truncation may potentially lead to incorrect results. See this comment
for details:

https://github.com/php/php-src/pull/8220#issuecomment-1073337678

Ilija

Reply via email to