The PHP spaceship operator used to compare two expressions according to 3 factors which are: less than, greater than, and equal to. This feature appeared in PHP 7.

In this quick tutorial, you will understand what is the PHP spaceship operator and how to use it with all PHP data types. Let’s get started.

The PHP spaceship operator is one of the PHP comparison operators. The first appearance for this operator was in PHP7, and that to detect the values according to three operators together, such as:

  • Less Than Operator — “<“.
  • Equal To Operator — “=“.
  • Greater Than Operator — “>“.

PHP spaceship operator takes 3 symbols together as this structure — “<=>“.

The major of the PHP comparison operators are returning a boolean value but here in the spaceship, it returns only three results with integer data types. Each one has a meaning based on the following three cases.

Spaceship ResultsThe MeaningDescriptions
1Greater ThanThis value refers to the first PHP variable being greater than the second one. So it returns 1.
0Equal ToTo expose that, two values of variables are matched in everything values and type. So it returns 0.
-1Less ThanThis value refers to the first variable being less than the second one. So it returns -1.

For a quick example

<?php
  var_dump( 5 <=> 4 ); // int(1)
?>

In the following sections, we are going to dive more into the spaceship with all PHP data types. Let’s discuss each one.

The PHP Spaceship Operator with Integers and Floats

In this part, we are going to expose how the spaceship works on the PHP integers.

In the following example, it will produce “1” as an integer result because the first variable is greater than the second variable.

<?php
  $x = 600;
  $y = 500;
  var_dump( $x <=> $y ); // int(1)
?>

But in case the first variable is less than the second one, it will print “-1”.

<?php
  $x = 300;
  $y = 500;
  var_dump( $x <=> $y ); // int(-1)
?>

And in case, the two variables are matched in values and data type. It will produce the “0” as an integer value.

<?php
  $x = 500;
  $y = 500;
  var_dump( $x <=> $y ); // int(0)
?>

Anyway, you can apply the same thing to the float numbers.

<?php 

  // Two both are matched
  var_dump( 11.5 <=> 11.5 ); // int(0)

  // Greater than
  var_dump( 11.5 <=> 10.6 ); // int(1)

  // Less than
  var_dump( 11.5 <=> 16.9 ); // int(-1)

?>

The Spaceship Operator with Strings

As you know, the PHP strings are a list of characters placed inside two quotations. Whatever, when you go to use the spaceship comparison the letters and characters are working according to the ASCII, So for more details, check the below list.

  • Uppercase characters in the alphabet started from 65 to 90 in the ASCII.
  • Lowercase characters in the alphabet started from 97 to 122 in the ASCII.

Actually, ASCII stands for the American Standard Code for Information Interchange, which means the encoded format for characters.

The following image shows you how ASCII works for the uppercase letters.

ASCII for Uppercase letters

And the following one for lowercase letters.

ASCII for Lowercase letters

Anyway, let’s take examples.

Case 1 — Less Than— “int(-1)”.

<?php 

  var_dump( "A" <=> "B" ); // int(-1)

  /* 
    Check the above figures 
    "A" => takes 65 in ASCII
    "B" => takes 66 in ASCII
    ------------------------------
    So A Less Than B in the ASCII
  */

   var_dump( "EK" <=> "xy" ); // int(-1)
    /* 
    Check the above figures 
    "EK" => take ( 69 + 75 ) in ASCII
    "xy" => take ( 120 + 121 ) in ASCII
    ------------------------------
    So [EK] Less Than [xy] in the ASCII
  */

?>

Case 2 — Equal To — “int(0)”.

<?php 
  
 // the two values are matched in the ASCII
  var_dump( "r" <=> "r" ); // int(0
  
?>

Case 3 — Greater Than — “int(1)”.

<?php 

  var_dump( "B" <=> "A" ); // int(1)

  /* 
    Check the above figures 
    "B" => takes 66 in ASCII
    "A" => takes 65 in ASCII
    ------------------------------
    So B Greater Than A in the ASCII
  */

  var_dump( "xy" <=> "EK" ); // int(1)
  /* 
    Check the above figures 
    "xy" => take ( 120 + 121 ) in ASCII
    "EK" => take ( 69 + 75 ) in ASCII
    ------------------------------
    So [xy] Greater Than [EK] in the ASCII
  */

?>

Let’s move to the boolean data type part.

The Spaceship Operator with Boolean

The boolean data type has only two boolean values, such as True and False. Logically, the truth value is bigger than the false value. And that, for this reason, the “True” is giving us “1” but the false value is giving us “0”. So 1 is greater than 0.

Let’s see an example.

<?php 

  // Two both are matched
  var_dump( false <=> false ); // int(0)

  // Greater than
  var_dump( true <=> false ); // int(1)

  // Less than
  var_dump( false <=> true ); // int(-1)

?>

In the following section, I will explain the spaceship for the arrays and objects

The Spaceship Operator with Arrays

The spaceship is comparing the array in values and data types. So if it contains a string value, it will compare it with ASCII. Check the below example

<?php 

   // two arrays has the same values 
   var_dump( array(1, 5, 6) <=> array( 1, 5, 6 ) ); // int(0) 

   // two arrays the first is bigger than the other one 
   var_dump( array(1, 5, 6) <=> array( 1, 6 ) ); // int(1) 

   // two arrays the first is less than the other one 
   var_dump( array( 5, 6) <=> array( 1, 6, 5 ) ); // int(-1) 

?>

The Spaceship Operator with Objects

The operator is the same comparison that is happening in the array. Here, I will do PHP casting for the previous arrays to objects before doing the comparison.

<?php 

   // two arrays has the same values 
   var_dump( (object) array(1, 5, 6) <=> (object) array( 1, 5, 6 ) ); // int(0) 

   // two arrays the first is bigger than the other one 
   var_dump( (object) array(1, 5, 6) <=> (object) array( 1, 6 ) ); // int(1) 

   // two arrays the first is less than the other one 
   var_dump( (object) array( 5, 6) <=> (object) array( 1, 6, 5 ) ); // int(-1) 

?>

Wrapping Up

In this tutorial, you understood what is the spaceship and how to detect the PHP data types such as integers, strings, arrays, objects, floats, and boolean. Also, you saw some real examples in each section.

For more details, navigate to PHP Manual.

Thank you for reading, stay tuned for my next tutorials.