Menu

Error Identifier: variable.unused

← Back to 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
{
	$a = 1;
}

Why is it reported? #

The variable $a is assigned a value but is never read on any code path. The assignment has no effect on the program’s behaviour. This usually indicates a logic error, a typo in the variable name, or leftover code from a refactoring.

This rule is part of PHPStan’s dead code analysis. It is reported at rule level 4 and above, and is currently part of Bleeding Edge.

How to fix it #

Remove the unused variable:

 function doFoo(): void
 {
-	$a = 1;
 }

Or use the variable if the value was meant to be consumed:

 function doFoo(): void
 {
 	$a = 1;
+	echo $a;
 }

If the assignment is a deliberate placeholder that you want to keep, prefix the variable name with an underscore. PHPStan ignores variables whose name starts with _:

-	$a = 1;
+	$_a = 1;

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\DeadCode\UnusedVariableRule [1]
Theme
A
© 2026 PHPStan s.r.o.