Menu

← Back to nullCoalesce.*

Error Identifier: nullCoalesce.variable

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
{
	$value = $undefined ?? 'default';
}

Why is it reported? #

The variable on the left side of the ?? (null coalesce) operator is never defined in the current scope. While PHP does not throw an error when using ?? with undefined variables (unlike a direct variable access), this typically indicates a bug – the variable name may be misspelled, or the code that was supposed to define it is missing.

How to fix it #

Define the variable before using it in the null coalesce expression:

 function doFoo(): void
 {
+	$defined = getData();
-	$value = $undefined ?? 'default';
+	$value = $defined ?? 'default';
 }

If the variable is intentionally optional (e.g., comes from an extract() call or a conditionally-executed block), restructure the code so the variable is always defined:

 function doFoo(bool $condition): void
 {
+	$result = null;
 	if ($condition) {
 		$result = compute();
 	}
 	$value = $result ?? 'default';
 }

How to ignore this error #

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

// @phpstan-ignore nullCoalesce.variable
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.variable

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]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.