The spaceship operator

This is a spaceship operator: "<=> ". It's still being discussed but there's a big chance you'll get to use it in PHP7.

The operator returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater.

Instead of this


function order_func($a, $b) {
    return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
}

you can do


function order_func($a, $b) {
    return $a <=> $b;
}

Join 9,500+ smart developers

Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share? Submit a link to the community section.