Shorthand comparisons in PHP

If you are a programmer then you may already know about comparison operators or shorthand comparisons.

Let’s understand how the ternary ?:, the null coalescing ?? and the spaceship <=> operators works in PHP.

  • The ternary operator is a shorten of if/else structures
  • The null coalescing operator is used to provide default values instead of null
  • The spaceship operator is used to compare two values

Ternary operator

The ternary operator is a shorthand for the if {} else {} structure. Instead of you write full if block:

<?php
$isAuthenticated = true;
if ($isAuthenticated) {
    $online = true;
} else {
    $online = false;
}

You can write this:

<?php
$online = $isAuthenticated ? true : false;

In above shorthand if statement, if $isAuthenticated is evaluated as true; $online will be set to true otherwise false.

PHP 5.3+ allows  to leave out the lefthand operand, allowing for even shorter expressions:

<?php
$online = $isAuthenticated ?: false;

Null coalescing operator

The null coalescing operator is available since PHP 7.0. It similar to the ternary operator but behaves like isset.

It is especially useful for arrays and assigning defaults values to the variable when it is not set.

<?php
$undefined ?? 'default'; // 'default'

$unassigned;
$unassigned ?? 'default'; // 'default'

$assigned = 'hello';
$assigned ?? 'default'; // 'hello'

'' ?? 'default'; // ''
'hello' ?? 'default'; // 'hello'
'0' ?? 'default'; // '0'
0 ?? 'default'; // 0
false ?? 'default'; // false

Coalescing operator takes two operands, and decide which of those to use based on the value of the lefthand operand.

Null coalescing on arrays

As it is mentioned earlier that, this operator is very useful in combination with arrays, let’s take a look at examples:

<?php
$user = [
    'username' => 'danyal',
    'roles' => [
        'id' => 120,
        'name' => 'Admin'
    ]
];

$input['username] ?? 'fallback'; // 'danyal'
$input['roles']['id'] ?? null; // 120
$input['undefined'] ?? 'fallback'; // 'fallback'
$input['roles']['undefined'] ?? 'fallback'; // 'fallback'

null ?? 'fallback'; // 'fallback'

Spaceship operator

The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b

<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
 
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

Author: Danyal
I'm skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting danya.dk