Menu

← Back to nullCoalesce.*

Error Identifier: nullCoalesce.expr

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
{
	echo rand() ?? 0; // ERROR: Expression on left side of ?? is not nullable.
}

Why is it reported? #

The ?? (null coalescing) operator provides a fallback value when the left side is null. PHPStan has determined that the expression on the left side of ?? can never be null, so the fallback value on the right side will never be used. This makes the ?? operator redundant.

In the example above, rand() always returns int, which is never null.

How to fix it #

Remove the null coalescing operator since the expression is never null:

 <?php declare(strict_types = 1);
 
 function doFoo(): void
 {
-	echo rand() ?? 0;
+	echo rand();
 }

If the expression genuinely should be nullable, update the return type of the function or method being called:

 <?php declare(strict_types = 1);
 
-function getValue(): int
+function getValue(): ?int
 {
-	return 42;
+	return rand(0, 1) === 0 ? null : 42;
 }

 echo getValue() ?? 0;

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Variables\EmptyRule [1]
  • PHPStan\Rules\Variables\IssetRule [1]
  • PHPStan\Rules\Variables\NullCoalesceRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.