Re: [RFC] Combined Comparison (Spaceship) Operator

From: Date: Thu, 22 Jan 2015 23:52:40 +0000
Subject: Re: [RFC] Combined Comparison (Spaceship) Operator
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi again,

> On 22 Jan 2015, at 23:47, Larry Garfield <[email protected]> wrote:
> 
> Assuming it's accurate and I'm understanding you correctly, I think the following
> would be a sufficient statement for the RFC:
> 
> -----
> The behavior of this operator with mixed types is such that the following two snippets produce
> the exact same result in all cases:
> 
> return $a <=> $b;
> 
> if ($a > $b) {
>  return -1;
> }
> elseif ($a < $b) {
>  return 1;
> }
> else {
>  return 0;
> }
> -----
> 
> Or maybe that's implicit in the existing link, I'm not sure.  (Now that I read it
> over again.) Basically, in order to understand the more esoteric type juggling that may occur
> you'd need to mentally expand it to its "uncompressed form", for which the rules are
> already defined (if possibly quirky in some cases).

Yes, that’s correct. Although it would be more accurate to say that ($a < $b) is just ($a
<=> $b === -1), ($a > $b) is just ($a <=> $b === 1) and similar things for >= and
<=, as that’s how the comparison operators work internally (with the exception of == which also
has a fast case).

I think I’ll add something along those lines to the RFC.

Thanks.
--
Andrea Faulds
http://ajf.me/






Thread (30 messages)

« previous php.internals (#81003) next »