website/errors/nullCoalesce.offset.md
<?php declare(strict_types = 1);
function doFoo(): void
{
$array = ['key' => 1];
echo $array['nonexistent'] ?? 'default';
}
The null coalesce operator (??) is used on an array offset that does not exist on the given type. The offset will never be found in the array, so the expression will always evaluate to the fallback value on the right side of ??.
This typically indicates one of two problems:
Fix the offset key to match what exists in the array:
$array = ['key' => 1];
-echo $array['nonexistent'] ?? 'default';
+echo $array['key'] ?? 'default';
If the offset genuinely might not exist, adjust the array type to reflect that:
-/** @var array{key: int} $array */
+/** @var array{key?: int} $array */