How to Use the Null Coalescing Operator (??) in PHP
$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
$<result> = <value> ?? <default>;
| Part | What 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:

<?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
php main.php
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

The command is the same in zsh, bash and PowerShell.
What Happened
$user['name'] ?? 'no name'gaveAda, because the key exists.- The missing
emailkey and the missingissproperty gave their defaults, without a warning. $timeout ?? 30gave30, because the variable holdsnull.- 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
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting ?? to replace false, 0 or '' | They aren't null, so they're used as they are | Use ?: 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 instead | Write ($a ?? 'x') . "\n" |
Using ?? where a value is required | A missing value is silently replaced | Check for it and report the problem |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Warning: Undefined array key "email" | The key was read without ?? or isset | Use $user['email'] ?? 'default' |
Warning: Undefined property: stdClass::$iss | The 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
- Null Coalescing Operator, in the PHP manual.
