Menu

← Back to empty.*

Error Identifier: empty.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
{
	empty($undefined);
}

Why is it reported? #

The variable used inside empty() is never defined in the current scope. The empty() language construct is intended to check whether a variable exists and has a non-empty value. When used on a variable that is never defined, the result is always true (because undefined variables are treated as empty). This usually indicates a logic error, such as a typo in the variable name or a missing assignment.

How to fix it #

Define the variable before using it with empty(), or fix the variable name if it is a typo:

 <?php declare(strict_types = 1);
 
 function doFoo(): void
 {
-	empty($undefined);
+	$value = getValue();
+	if (empty($value)) {
+		// handle empty case
+	}
 }

If the intent is to check whether an optional variable has been set, use isset() instead with proper variable initialization:

 <?php declare(strict_types = 1);
 
-function doFoo(): void
+function doFoo(?string $value = null): void
 {
-	empty($undefined);
+	if ($value === null || $value === '') {
+		// handle missing/empty case
+	}
 }

How to ignore this error #

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

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