Search code examples
phpspaceship-operator

Does php spaceship ever return something other than -1, 0, or 1?


According to this, the spaceship operator (<=>) returns "an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y".

Trying it out, it seems to only return -1, 0, or 1.

Is that always the case?


Solution

  • From PHP's New feature's page:

    Spaceship operator

    The spaceship operator is used for comparing two expressions.
    It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.
    Comparisons are performed according to PHP's usual type comparison rules.

    So, only -1, 0 or 1 can be returned from <=>