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 a skilled programmer specializing in Vue.js/Nuxt.js for front-end development and PHP Laravel for back-end solutions. I have a strong focus on API design and development, complemented by experience in web server setup and maintenance. My versatile expertise ensures seamless creation and maintenance of web applications, covering everything from intuitive user interfaces to robust server-side functionality. Passionate about coding and driven by a lifelong learning mindset, I invite you to explore more at danyal.dk.