Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Coalesce And The Fatal Error

<?php

$a = 1;
$b = $a['c'] ?? 'b';
$b = $a->c ?? 'b';

$a = [];
$b = $a->f ?? 'b';

$a = new stdclass();
$b = $a['f'] ?? 'b';

The coalesce operator is based on the isset() feature of PHP. As such, it is prone to the same issues. This is the case with a sneaky fatal error.

Usually, coalesce hide any existence check on the left operand: that way, the default value may be used without a noisy warning. This does not apply when trying to check an array index on an object: it actually yields a fatal error.

Such situation is usually rare, as the structure of the variable is usually known to some extend. On the other hand, using ?? on the result of an unstructured dataset, such as from a JSON or YAML (or else), it becomes far more hazardous.

Note that json_decode() has an option to generate an object or an array. In such situation, it is safer to generate an array, which does not yield fatal errors. It might also be faster to run.

See Also

PHP Error Messages

PHP Features

Last updated: 10 September 2026