Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Fallback for null (??)

Read a value when it exists and is not null; otherwise evaluate the fallback.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

$settings['key'] ?? $fallback

Read a value when it exists and is not null; otherwise evaluate the fallback. A missing key can be read this way without an undefined-key warning. Zero, false and the empty string are already valid values and survive. This makes the operator suitable for optional settings that may deliberately disable a feature.

$settings['key'] ?? $fallback
[0,"guest",1]

Examples

<?php
$settings = ['limit'=>0, 'name'=>null];
echo json_encode([$settings['limit'] ?? 10, $settings['name'] ?? 'guest', $settings['page'] ?? 1]);

Result

[0,"guest",1]

Sources