Kode Blog - Inspiring And Empowering Developers.

Home About Us Courses Categories Blog Contact Us

PHP 7 New Operators


PHP 7 gives us two more new operators namely the combined comparison (spaceship operator) and null coalescing. Both of these operators are used to compare values. This tutorial will introduce you to the spaceship and null coalescing operators and look at practical examples that will show you how to use them.

PHP 7 Spaceship Operator

Topics to be covered

We will cover the following topics

  • Tutorial pre-requisites
  • What is the Spaceship operator?
  • PHP 7 Spaceship operator example
  • What is the null coalescing operator?
  • PHP Null coalescing example

Tutorial Pre-requisites

For you to successfully complete this tutorial, you should have/know the following.

  • This tutorial assumes that you know the basics of PHP.
  • You have an IDE such as NetBeans or a text editor i.e. Sublime Text, Brackets etc.
  • You have PHP 7 and a web server already installed and running.
  • You have a modern web browser
  • You read the previous tutorial that introduces PHP 7. This is optional but highly recommended.

What is the spaceship operator?

The spaceship operator also known as the combined comparison is an operator that compares two values and returns; 1. -1 if the first value is less than the second value 2. 0 if both values are equal 3. 1 if the first value if greater than the second value

The spaceship operator is represented using the symbol <=>. The combination of the less than, equal, and greater than operators resemble a spaceship hence the name spaceship.

The following table shows the truth table of a spaceship operator.

PHP 7 Spaceship Operator

As you can see from the above results, we are evaluating everything using the first value. Here is an easy way of memorizing the results.

  • Always evaluate using the first value (left side)
  • If the first value is less the other value, then the result is a negative (-). Less and negative kind of go together
  • If the first value is greater than the other value, then the result is a positive (+). Greater and positive kind of go together.

The major practical application of the spaceship operator is when writing usort functions for sorting arrays. We will talk more about the practical application in the next section.

PHP 7 Spaceship operator example

Let’s start with a very basic example.

Step 1 – Create a new project

I am using XAMPP on windows so I will just create a new directory php7-new-operators in C:\htdocs\xampp

Step 2 – Create two files

  • spaceship_basic.php
  • usort.php
  • spaceship_usort.php

Step 3 - Code for spaceship_basic.php

  1. Open spaceship_basic.php
  2. Add the following code

    echo "1 <=> 1 produces: " . ( 1 <=> 1) . "<br/>";
    echo "1 <=> 2 produces: " . ( 1 <=> 2) . "<br/>";
    echo "2 <=> 1 produces: " . ( 2 <=> 1) . "<br/>";

Load the following URL into your web browser


You will get the following results

1 <=> 1 produces: 0
1 <=> 2 produces: -1
2 <=> 1 produces: 1

Step 4 – Code for usort.php

The usort function is used to sort array values using a user defined function. The major use of the spaceship operator is when writing the user defined function that sorts the array. For us to appreciate the spaceship operator, we must first look at how the implementation is done using lower versions of PHP

Open usort.php

Add the following code


$beverages = array(
    array('beverage' => 'Coke', 'price' => 5),
    array('beverage' => 'Water', 'price' => 2),
    array('beverage' => 'Vodka', 'price' => 21)

echo "Beverages before sorting <br>";


echo "<br>";

usort($beverages,function ($a, $b) {
    return ($a['price'] < $b['price']) ? -1 : (($a['price'] > $b['price']) ? 1 : 0);

echo "Beverages after sorting <br>";



  • $beverages = array(…) defines an of beverages and their prices
  • echo…, print_r… prints the values of the $beverages array before and after sorting
  • usort($beverages,function ($a, $b) {…} uses an anonymous function to sort the array values. usort expects the returned result to be -1, 0, or 1. The anonymous function return ($a['price'] < $b['price']) ? -1 : (($a['price'] > $b['price']) ? 1 : 0); cleverly uses the less than operator to return -1 and the ternary operator to return either 0 or 1. The code works but it’s too verbose.

Step 5 – Code for spaceship_usort.php

The spaceship operator allows us to fix the above problem with simplicity.

  1. Open spaceship_usort.php
  2. Add the following code

    $beverages = array(
        array('beverage' => 'Coke', 'price' => 5),
        array('beverage' => 'Water', 'price' => 2),
        array('beverage' => 'Vodka', 'price' => 21)
    echo "Beverages before sorting <br>";
    echo "<br>";
    usort($beverages,function ($a, $b) {
        return $a['price'] <=> $b['price'];
    echo "Beverages after sorting <br>";


  • We replaced ($a['price'] < $b['price']) ? -1 : (($a['price'] > $b['price']) ? 1 : 0); with the simple code $a['price'] <=> $b['price']; and still got the same results.

What is the null coalescing operator?

The null coalescing operator is used to check for the value of a variable and return a default value of the value of the variable is null. In simple terms, it can be stated as if x is null then return default value.

PHP 7 Null coalescing example

Let’s first start by looking at how the problem that the null coalescing operator solves before we look at the null coalescing operator itself.

if (isset($_GET['term'])){
    $search_term = $_GET['term'];
    $search_term = '';

We are using the isset(x) function to check for the existing of $_GET['term'] then assigning the value to $search_term if the variable exists. If it does not exist, then we set a default value of an empty string.

Alternative the above code can be written using the ternary operator as

$search_term = isset($_GET['term']) ? $_GET['term'] : '';

Null coalescing to the rescue

The null coalescing operator combines both the isset and ternary operator functions into one.

The null coalescing operator uses two question marks. In a nutshell, the syntax is as follows

$value = $variable_value ?? $default_value;


  • If $variable_value is null or undefined then return $default_value

The above code can be rewritten using the null coalescing operator as

$search_term = $_GET['term'] ?? '';

Another advantage of the null coalescing operator is that it allows us for check for more than one value. Let’s say you want to capture a variable that can be submitted using $GET, $POST or $_PUT

You can use the following expression

$value = $_GET['value'] ?? $_POST['value'] ?? $_PUT['value'] ?? '';


  • If $_GET['value'] is null then return $_POST['value']. If $_POST['value'] is null then return $_PUT['value']. If $_PUT['value'] is null then return the default value ''.


The spaceship and null coalescing are comparison operators that help us write better code. They do this by combination a number of comparisons and checks into a single operator.

What’s Next?

The next tutorial will look at new operators in PHP 7 Anonymous Classes and PHP 7 Group Use.

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Tutorial History

Date Published: 2016-05-22