➕ PHP Operators
Estimated reading: 3 minutes 348 views

PHP Spaceship Operator <=> – Full Guide with Examples

Introduction – Why the Spaceship Operator Matters

Introduced in PHP 7, the spaceship operator (<=>) simplifies three-way comparisons — especially useful when sorting arrays or evaluating numeric and string expressions compactly.

Instead of writing multiple if-else conditions to check whether a value is less than, equal to, or greater than, you can now do it with one elegant expression.

In this guide, you’ll learn:

  • What the spaceship operator is
  • How it works with numbers, strings, and arrays
  • How to use it in sorting functions
  • Practical examples and explanations

What Is the Spaceship Operator?

The spaceship operator performs a three-way comparison and returns:

ExpressionResult
$a < $b-1
$a == $b0
$a > $b1

Syntax:

$a <=> $b

Practical Examples with Explanations

1. Numeric Comparison

echo 5 <=> 10;  // Output: -1
echo 10 <=> 10; // Output: 0
echo 15 <=> 10; // Output: 1

Explanation:

  • 5 < 10-1
  • 10 == 100
  • 15 > 101

2. String Comparison

echo "apple" <=> "banana"; // Output: -1
echo "grape" <=> "grape";  // Output: 0
echo "pear" <=> "orange";  // Output: 1

Explanation:

  • PHP uses lexicographical (dictionary-style) comparison for strings.

3. Using in usort() for Array Sorting

$numbers = [5, 2, 8, 1, 3];

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

print_r($numbers);

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
    [4] => 8
)

Explanation:
usort() uses the spaceship operator to sort in ascending order.


4. Descending Sort with <=>

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

Explanation:
Just reverse the operands to get descending sort.


When to Use the Spaceship Operator

Perfect for:

  • Sorting arrays (usort, uasort, uksort)
  • Replacing verbose if-else chains like:
if ($a < $b) return -1;
elseif ($a > $b) return 1;
else return 0;

Limitations and Notes

  • Only available in PHP 7.0 and above
  • Operands must be comparable (numbers, strings, etc.)
  • For complex object comparison, customize logic using comparator methods

Summary – Recap & Next Steps

The PHP spaceship operator (<=>) is a smart and clean way to compare two values, returning -1, 0, or 1. It’s especially handy for sorting tasks and streamlining comparisons in custom functions.

Key Takeaways:

  • a <=> b returns -1, 0, or 1 for less than, equal, or greater
  • Ideal for sorting arrays efficiently
  • Works with numbers, strings, and even floats

Real-World Relevance:
Used in e-commerce filtering, leaderboard sorting, rating comparison, and any ordered dataset.


Frequently Asked Questions (FAQs)

What does <=> mean in PHP?
It’s the spaceship operator — returns -1, 0, or 1 based on how $a compares to $b.

Can it be used with strings?
Yes, it works like strcmp() for lexicographic comparison.

Is <=> faster than writing if-else comparisons?
Yes. It’s more concise and avoids branching overhead.

What PHP version supports <=>?
PHP 7.0 and above.


Share Now :
Share

🚀 PHP Spaceship Operator

Or Copy Link

CONTENTS
Scroll to Top