Error Identifier: isset.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
{
if (isset($undefined)) { // ERROR: Variable $undefined in isset() is never defined.
echo $undefined;
}
}
Why is it reported? #
The variable checked inside isset() is never defined in the current scope. The isset() language construct checks whether a variable exists and is not null. When used on a variable that is never defined, the result is always false. This usually indicates a typo in the variable name or a missing assignment.
How to fix it #
Define the variable before checking it, or fix the variable name if it is a typo:
<?php declare(strict_types = 1);
function doFoo(): void
{
- if (isset($undefined)) {
- echo $undefined;
+ $value = getValue();
+ if (isset($value)) {
+ echo $value;
}
}
If the variable comes from a dynamic source (e.g., extracted variables), make the data flow explicit:
<?php declare(strict_types = 1);
-function doFoo(): void
+function doFoo(?string $name = null): void
{
- if (isset($undefined)) {
- echo $undefined;
+ if (isset($name)) {
+ echo $name;
}
}
How to ignore this error #
You can use the identifier isset.variable to ignore this error using a comment:
// @phpstan-ignore isset.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: isset.variable