PHP Spaceship Operator

Last updated on

The “Spaceship Operator” in PHP, also known as the combined comparison operator, was introduced in PHP 7. It is represented by the <=> symbol and is used for comparing two expressions. This operator returns one of three values:

  • 0 if both operands are equal,
  • -1 if the left operand is less than the right operand,
  • 1 if the left operand is greater than the right operand.

The spaceship operator is particularly useful when sorting arrays. Prior to PHP 7, developers had to use custom sorting functions with usort(), but the introduction of the spaceship operator made the code more concise and readable.

Let’s examine its fundamental syntax.

PHP Spaceship Operator Syntax

The syntax of the PHP Spaceship Operator (<=>) involves comparing two values. Here is the basic structure:

<?php
$result = $value1 <=> $value2;
?>

In this syntax:

  • $value1 is the left operand.
  • $value2 is the right operand.
  • $result will be:0 if $value1 is equal to $value2.-1 if $value1 is less than $value2.1 if $value1 is greater than $value2.
  • 0 if $value1 is equal to $value2.
  • -1 if $value1 is less than $value2.
  • 1 if $value1 is greater than $value2.

Anyway, let’s see more examples.

PHP Spaceship Operator Examples

Here’s a basic example of how the spaceship operator can be used for comparisons:

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

$result = $a <=> $b;

if ($result == 0) {
    echo "$a is equal to $b";
} elseif ($result == -1) {
    echo "$a is less than $b";
} else {
    echo "$a is greater than $b";
}
?>

In this example, $result will be -1 since $a is less than $b, and the corresponding message will be displayed.

The common use case for the spaceship operator is in sorting arrays. Let’s say you have an array of numbers and you want to sort them using the spaceship operator:

<?php

$numbers = [4, 2, 8, 1, 6];

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

print_r($numbers);

?>

After the usort function is applied with the spaceship operator, the $numbers array will be sorted in ascending order.

Let’s explore how to use the PHP spaceship operator with strings.

Using the PHP Spaceship Operator with Strings (ASCII)

Comparing strings differs from numeric, float, or other data types. Moreover, the PHP spaceship operator compares strings based on ASCII (American Standard Code for Information Interchange).

The ASCII is indeed a character encoding standard that assigns numerical values to letters, digits, and symbols to facilitate their representation in computers and other devices. Consequently, when comparing strings using the Spaceship Operator in PHP, it’s the underlying ASCII values of the characters that are being compared.

For example, in ASCII, the character ‘a’ has a lower numerical value than ‘b’, and so on. Therefore, when you use the Spaceship Operator to compare two strings, it essentially compares the strings character by character based on their ASCII values.

The following image illustrates how ASCII works for uppercase letters.

uppercase letters

And the next one for lowercase letters.

lowercase letters

Here are examples to illustrate the concepts discussed in the above paragraphs:

<?php

// Example of comparing strings using the Spaceship Operator in PHP
$string1 = "apple";
$string2 = "banana";

$result = $string1 <=> $string2;

// The result will be -1, indicating that "apple" comes before "banana" in ASCII order.
echo "Result of string comparison: $result";

?>

Explanation: In this example, the Spaceship Operator (<=>) is used to compare two strings ($string1 and $string2). The result is -1, which means that “apple” comes before “banana” in ASCII order.

<?php
// Example demonstrating ASCII values of characters
$charA = 'a';
$charB = 'b';

// Using ord() function to get ASCII value
$asciiA = ord($charA);
$asciiB = ord($charB);

echo "ASCII value of $charA: $asciiA, ASCII value of $charB: $asciiB";
?>

In this example, the ord() function is used to obtain the ASCII values of the characters ‘a’ and ‘b’. In ASCII, ‘a’ has a lower numerical value than ‘b’, and the output will display the ASCII values accordingly.

Let’s summarize it.

Wrapping Up

the ‘Spaceship Operator’ in PHP, introduced in PHP 7, provides a concise and expressive means of comparing two expressions. Its three possible return values (-1, 0, 1) make it particularly effective for sorting arrays, simplifying code that previously required custom sorting functions.

Moving on to the basic syntax of the Spaceship Operator, it involves comparing two values and producing outcomes based on their relationship. Examples demonstrate its usage, including sorting arrays for numeric values. Moreover, the operator’s versatility extends to string comparisons, where it leverages ASCII values for precise character-by-character evaluations.

Understanding the ASCII (American Standard Code for Information Interchange) encoding standard is fundamental to comprehending how the Spaceship Operator functions with strings. This character encoding assigns numerical values to letters, digits, and symbols, enabling effective string comparisons in PHP.

Illustrative examples provided showcase practical applications of the Spaceship Operator, both in numeric and string contexts. From sorting arrays to comparing strings based on ASCII values, the operator proves its utility across various scenarios.

Share on: