Menu

← Back to variable.*

Error Identifier: variable.dynamicName

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
{
    $name = 'foo';
    echo $$name;
}

Why is it reported? #

Variable variables ($$name) use the value of one variable as the name of another variable. While this is valid PHP syntax, it makes code harder to understand, maintain, and analyse statically. PHPStan cannot reliably track the types of variable variables because their names are determined at runtime. Variable variables also make it easy to introduce bugs through typos in string values.

Another common form of variable variables is assignment:

<?php declare(strict_types = 1);

function doBar(): void
{
    $name = 'foo';
    $$name = 'bar'; // equivalent to $foo = 'bar'
}

How to fix it #

Replace the variable variable with an associative array:

 <?php declare(strict_types = 1);
 
 function doFoo(): void
 {
-    $name = 'foo';
-    echo $$name;
+    $data = ['foo' => 'some value'];
+    $name = 'foo';
+    echo $data[$name];
 }

Or use the variable directly if the name is known:

 <?php declare(strict_types = 1);
 
 function doFoo(): void
 {
-    $name = 'foo';
-    echo $$name;
+    $foo = 'some value';
+    echo $foo;
 }

This error is reported by the phpstan-strict-rules extension.

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\VariableVariables\VariableVariablesRule [1] phpstan/phpstan-strict-rules

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.