PHPStorm: undefined variables caused by include/require PHPStorm: undefined variables caused by include/require php php

PHPStorm: undefined variables caused by include/require


All above answers removes or suppress warnings which imho is not good solution.

Best solution is to add header with doc block with used variables.

Example:

<?php/** * @var string $name * @var FormData $form */?>

This will not only prevent from showing "Undefined variable" warning, but also document your code and makes autocomplete working as expected.


As hakre pointed out, this warning could be a sign of bad code smell. However, you can disable PHPStorm's inspection of undefined variables if there is a prior include or require call.

Uncheck the radio button in Settings/Editor/Inspections/PHP/Undefined variable called "Ignore 'include' and 'require' statements".

enter image description here

If you don't want to disable this inspection, you can disable it for a single statement by using PHPStorm's @noinspection annotation. Example code:

require $path; // $some_variable is defined in that file/** @noinspection PhpUndefinedVariableInspection */$config["DBSettings"] = [    "server" => $some_variable];


Enable "Search for variable's defination outside the current file"

enter image description here