Menu

← Back to variable.*

Error Identifier: variable.undefined

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 greet(): string
{
	return 'Hello, ' . $name;
}

Why is it reported? #

The variable being used has not been defined in the current scope. This usually means the variable name is misspelled, the variable was intended to be a parameter, or the code path that assigns the variable is not always reached.

How to fix it #

Make sure the variable is defined before it is used.

 <?php declare(strict_types = 1);
 
-function greet(): string
+function greet(string $name): string
 {
 	return 'Hello, ' . $name;
 }

If the variable might not be defined depending on the code path, assign a default value:

 <?php declare(strict_types = 1);
 
 function summarize(bool $verbose): string
 {
+	$detail = '';
 	if ($verbose) {
 		$detail = 'Full details here';
 	}

 	return $detail;
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Variables\CompactVariablesRule [1] [2]
  • PHPStan\Rules\Variables\DefinedVariableRule [1] [2]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.