On Thu, Feb 13, 2014 at 3:19 PM, Christian Stoller <[email protected]>wrote:
> Davey Shafik <[email protected]> wrote:
> > This adds a new operator "(expr) <=> (expr)" that returns 0 if both
> > operands are equal, 1 if the left is greater, and -1 if the right is
> > greater.
> >
> > It works with all types (just as well as <, <=, >=, > work) and is great
> > for usort() callbacks for example.
> >
>
> Why not using $a - $b
instead of $a <=> $b
?
>
This only works on numbers, and integers in particular, because of how the
result of a comparison function is cast.
For example, 0.4f - 0.5f
equals -0.1f
but gets cast to 0
when
it's
converted to a long value.
See also: http://lxr.php.net/xref/PHP_5_6/ext/standard/array.c#597
>
> Simon J Welsh <[email protected]> wrote:
> > The only real case I see for this is to save some boilerplate when
> > dealing with arrays. Strings have strcmp(), numbers have subtraction
> > And when you're sorting objects, you probably want to be doing
> > the comparison on some string/numeric property.
> >
>
> Agreed! It would make more sense to write a comparison function for
> arrays/objects.
>
>
> Best regards
> Christian
>
--
--
Tjerk