Menu

Error Identifier: nullCoalesce.offset

← Back to nullCoalesce.*

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

function doFoo(): void
{
	$array = ['key' => 1];

	echo $array['nonexistent'] ?? 'default';
}

Why is it reported? #

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:

  • The offset key is misspelled
  • The variable does not contain the expected array shape

How to fix it #

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 */

How to ignore this error #

You can use the identifier nullCoalesce.offset to ignore this error using a comment:

// @phpstan-ignore nullCoalesce.offset
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: nullCoalesce.offset

Rules that report this error #

  • PHPStan\Rules\Variables\EmptyRule [1] [2] [3]
  • PHPStan\Rules\Variables\IssetRule [1] [2] [3]
  • PHPStan\Rules\Variables\NullCoalesceRule [1] [2] [3]
Theme
A
© 2026 PHPStan s.r.o.