Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP

PHP's double question mark, or the null coalescing operator

Table of contents:

PHP's double question mark, or the null coalescing operator

The null coalescing operator (??), or the double question mark

The null coalescing operator, or double question mark, was introduced in PHP 7.0 and is a handy shortcut that helps you write cleaner and more readable code. It’s represented by two question marks ??.

Let’s say you want to get a value from a variable, but if that variable is not set or is null, you want to use a default value instead. You can use the ?? operator to do this in one step.

For example:

$name = $_GET['name'] ?? 'Unknown';

This line of code will set $name to $_GET['name'] if it’s set and not null. Otherwise, it will set $name to “Unknown”.

You can also chain them together like this:

$foo = $foo ?? $bar ?? 'baz';

This will check $foo first, then $bar, and use “baz” if neither are set and not null.

The null coalescing assignment operator (??=), or the double question mark equals

PHP 7.4 introduced a new shortcut, ??= (double question mark equals), also called the null coalescing assignment operator. This is used when you want to set a variable to a new value only if it’s currently not set or null.

It’s hard to make up a good example, but here’s a simplified one from this very blog’s codebase:

function do_something(DateTime $from, DateTime $to = null)
{
// Using the ternary operator.
$to = $to ? $to : new DateTime('now');
 
// Using the Elvis operator.
$to = $to ?: new DateTime('now');
 
// Using the null coalescing assignment operator.
$to ??= new DateTime('now');
 
// Do something.
}

This will set $now to a new DateTime instance only if it’s not already set (or null in that case).

Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

PHP 8.3 is out, now! Here's what's new and changed. PHP 8.3 is out, now! Here's what's new and changed.

PHP 8.3 was released on November 23, 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

Modified on Nov 23, 2023

PHP
Fix "Invalid argument supplied for foreach" in PHP & Laravel Fix "Invalid argument supplied for foreach" in PHP & Laravel

Learn why the "Invalid argument supplied for foreach()" warning happens, and let me show you multiple ways to fix it.

Modified on Nov 23, 2022

20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

Laravel interview questions and answers for 2023 Laravel interview questions and answers for 2023

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Modified on Sep 19, 2023 Audio available

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -