Home ยป PHP Spaceship Operator Tutorial with Examples

PHP Spaceship Operator Tutorial with Examples

The spaceship operator (<=>) is a comparison operator introduced in PHP 7. It is also known as the combined comparison operator. This operator compares two values and returns:

  • -1 if the value on the left is less than the value on the right,
  • 0 if both values are equal,
  • 1 if the value on the left is greater than the value on the right.

The spaceship operator can be used for numbers, strings, and arrays. It is commonly used in sorting algorithms or any situation where you need to compare values.

Syntax:

$result = $a <=> $b;

How the PHP Spaceship Operator Works

Return Values of the Operator:

  • $a <=> $b returns:
    • -1 if $a < $b
    • 0 if $a == $b
    • 1 if $a > $b

1. Spaceship Operator with Integers

The spaceship operator works with integers to compare numeric values.

Example:

<?php
$a = 5;
$b = 10;

echo $a <=> $b; // Outputs: -1 (because 5 is less than 10)

$a = 15;
$b = 15;

echo $a <=> $b; // Outputs: 0 (because 15 is equal to 15)

$a = 20;
$b = 10;

echo $a <=> $b; // Outputs: 1 (because 20 is greater than 10)
?>
  • In this example:
    • When $a is less than $b, the spaceship operator returns -1.
    • When $a equals $b, it returns 0.
    • When $a is greater than $b, it returns 1.

2. Spaceship Operator with Floats

The spaceship operator works with floating-point numbers as well.

Example:

<?php
$a = 5.75;
$b = 10.50;

echo $a <=> $b; // Outputs: -1 (because 5.75 is less than 10.50)

$a = 10.50;
$b = 10.50;

echo $a <=> $b; // Outputs: 0 (because 10.50 is equal to 10.50)

$a = 15.25;
$b = 10.50;

echo $a <=> $b; // Outputs: 1 (because 15.25 is greater than 10.50)
?>
  • The spaceship operator returns the same results for floating-point numbers, comparing their magnitude just like with integers.

3. Spaceship Operator with Strings

The spaceship operator can also be used to compare strings. String comparisons are case-sensitive and are based on lexicographical order.

Example:

<?php
$a = "apple";
$b = "banana";

echo $a <=> $b; // Outputs: -1 (because "apple" comes before "banana" lexicographically)

$a = "orange";
$b = "orange";

echo $a <=> $b; // Outputs: 0 (because both strings are the same)

$a = "pear";
$b = "grape";

echo $a <=> $b; // Outputs: 1 (because "pear" comes after "grape" lexicographically)
?>
  • Here, strings are compared based on their lexicographical (dictionary) order:
    • “apple” comes before “banana”, so the result is -1.
    • “orange” is equal to “orange”, so the result is 0.
    • “pear” comes after “grape”, so the result is 1.

4. Spaceship Operator with Arrays

The spaceship operator can also be used to compare arrays. It compares arrays in the following manner:

  • The first elements are compared.
  • If the first elements are equal, the second elements are compared, and so on.
  • If all elements are equal, it returns 0.

Example:

<?php
$a = [1, 2, 3];
$b = [1, 2, 4];

echo $a <=> $b; // Outputs: -1 (because 3 is less than 4)

$a = [1, 2, 3];
$b = [1, 2, 3];

echo $a <=> $b; // Outputs: 0 (because both arrays are identical)

$a = [2, 3, 4];
$b = [1, 3, 4];

echo $a <=> $b; // Outputs: 1 (because 2 is greater than 1)
?>
  • The spaceship operator compares arrays element by element. In this example:
    • The first comparison finds that the third element of $a is less than the third element of $b, so the result is -1.
    • If all elements are equal, the result is 0.
    • If the first differing element in $a is greater than in $b, the result is 1.

5. Spaceship Operator in Sorting

One of the most practical uses of the spaceship operator is in sorting arrays. You can use it in custom comparison functions to sort arrays in ascending or descending order.

Example (Custom Sorting with Numbers):

<?php
$numbers = [5, 3, 9, 1, 7];

// Sort numbers using spaceship operator
usort($numbers, function($a, $b) {
    return $a <=> $b; // Ascending order
});

print_r($numbers); // Outputs: Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 )
?>
  • Here, usort() is used to sort an array of numbers in ascending order. The spaceship operator (<=>) compares the numbers, and the sorting is based on the comparison.

Example (Custom Sorting with Strings):

<?php
$fruits = ["orange", "apple", "banana", "grape"];

// Sort strings using spaceship operator
usort($fruits, function($a, $b) {
    return $a <=> $b; // Lexicographical order
});

print_r($fruits); // Outputs: Array ( [0] => apple [1] => banana [2] => grape [3] => orange )
?>
  • In this example, usort() uses the spaceship operator to sort an array of strings lexicographically.

Example (Custom Sorting in Descending Order):

<?php
$numbers = [5, 3, 9, 1, 7];

// Sort numbers in descending order
usort($numbers, function($a, $b) {
    return $b <=> $a; // Descending order
});

print_r($numbers); // Outputs: Array ( [0] => 9 [1] => 7 [2] => 5 [3] => 3 [4] => 1 )
?>
  • Here, the spaceship operator is used to sort the array of numbers in descending order by reversing the comparison ($b <=> $a).

Key Takeaways for the PHP Spaceship Operator:

  1. Comparison of Values:
    • $a <=> $b returns:
      • -1 if $a is less than $b.
      • 0 if $a is equal to $b.
      • 1 if $a is greater than $b.
  2. Data Types:
    • The spaceship operator works with integers, floats, strings, and arrays.
  3. Common Use Case:
    • Sorting: The spaceship operator is commonly used in sorting algorithms or when comparing elements in custom functions.

Conclusion

The spaceship operator (<=>) is a powerful tool in PHP, especially when working with sorting, comparison operations, and handling complex comparisons. It simplifies comparisons by returning a clear, concise result (-1, 0, or 1), and can be used with various data types like numbers, strings, and arrays.

By mastering the spaceship operator, you can write cleaner, more efficient code for comparisons and sorting tasks!

You may also like