Skip to main content

How to Use the Null Coalescing Operator (??) in PHP

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

$a ?? $b in PHP gives $a when it exists and isn't null, and $b otherwise. It's the short way to give a missing array key or object property a default, without the warning PHP prints when you read one that isn't there.

Syntax​

Syntax
$<result> = <value> ?? <default>;
PartWhat it does
<value>What you'd like to use: a variable, an array element such as $user['email'] or a property such as $claims->iss.
??Uses <value> when it exists and isn't null, and <default> otherwise.
<default>The value to fall back on.

The PHP manual: "The expression (expr1) ?? (expr2) evaluates to expr2 if expr1 is null, and expr1 otherwise. In particular, this operator does not emit a notice or warning if the left-hand side value does not exist, just like isset()."

The Example​

The example reads an array key that exists and one that doesn't, a property an object doesn't have, and a variable that holds null, each with ??. At the end, it reads the missing array key without ??.

Prerequisites​

  • PHP 7 or later. This guide uses PHP 8.6.0.

Step 1: Write the Code​

Create a file named main.php:

main.php, which echoes user name and user email with defaults via ??, a missing iss property of a stdClass with a default, a null timeout with the default 30, and finally user email without ??.

main.php
<?php

$user = ['name' => 'Ada'];
echo ($user['name'] ?? 'no name') . "\n";
echo ($user['email'] ?? 'no email') . "\n";

$claims = new stdClass();
$claims->sub = 'ada';
echo ($claims->iss ?? 'no issuer') . "\n";

$timeout = null;
echo ($timeout ?? 30) . "\n";

echo $user['email'] . "\n";

The parentheses around each ?? expression make it run before the . that adds the line break.

Step 2: Run It​

Terminal
php main.php
Output
Ada
no email
no issuer
30
PHP Warning: Undefined array key "email" in /Users/Shared/you/main.php on line 14

Warning: Undefined array key "email" in /Users/Shared/you/main.php on line 14

A zsh terminal where php main.php prints Ada, no email, no issuer and 30, then a warning about the undefined array key email, twice.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • $user['name'] ?? 'no name' gave Ada, because the key exists.
  • The missing email key and the missing iss property gave their defaults, without a warning.
  • $timeout ?? 30 gave 30, because the variable holds null.
  • Reading $user['email'] without ?? printed a warning and an empty value. It appears twice because this PHP's settings both display errors and log them to the terminal.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting ?? to replace false, 0 or ''They aren't null, so they're used as they areUse ?: when every "empty" value should get the default
Leaving out parentheses in echo $a ?? 'x' . "\n". runs first, so the line break joins the default insteadWrite ($a ?? 'x') . "\n"
Using ?? where a value is requiredA missing value is silently replacedCheck for it and report the problem

Troubleshooting​

SymptomLikely causeFix
Warning: Undefined array key "email"The key was read without ?? or issetUse $user['email'] ?? 'default'
Warning: Undefined property: stdClass::$issThe property was read without ??Use $claims->iss ?? 'default'

FAQ​

Is ?? the same as isset?​

For reading a value, yes: the manual says it works "just like isset()". $a ?? $b is short for "$a if it's set and not null, otherwise $b".

Is there a short way to set a default on a variable?​

Yes, ??= assigns only when the variable is missing or null: $timeout ??= 30;.

Conclusion​

?? gives a missing or null value a default without a warning. Use it for optional array keys and object properties, and keep checks for the values that must be there.

Additional Resources​