โž• PHP Operators
Estimated reading: 3 minutes 38 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 == 10 โ†’ 0
  • 15 > 10 โ†’ 1

๐Ÿ”ค 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿš€ PHP Spaceship Operator

Or Copy Link

CONTENTS
Scroll to Top