57

PHPStorm showed that all the variables from other files, both required and included, are undefined. I found this solution here, but after I disabled that option Ignore 'include' and 'require' statements, the IDE ignored all undefined variables.

For example, I have a file a.php with content $name = 'Bob', and file b.php, which require the file a.php. When I type echo $name in file b.php it works as expected and displays 'Bob'. The IDE, however, highlights the variable $name claiming it's undefined. If I disable that option 'Undefined variable' - Ignore 'include' and 'require' statements, the IDE stops highlighting it.

With that option I can now write any variables, for example $sdadasdasdas in file b.php and the IDE doesn't highlight it.

Can PHPStorm understand which variables are set in included/required files and which ones are not?

2
  • I think this a similar question [Undefined variable PDO][1]. I use it in PHPStorm 8. [1]: stackoverflow.com/questions/20036690/undefined-variable-pdo/… Commented Dec 13, 2014 at 6:09
  • So annoying, can't see anything in the side bar in historical code because it is full of annoying and uninteresting warnings. Commented Aug 17, 2023 at 9:47

6 Answers 6

87

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Yeah! you are correct! I like this solution because don't modify the code base in my share repository, I mean code with other annotation that other programmers don't matter.
@Pawel Hawro This worked for me. I was getting frustrated because I instantiated an object in an init.php, then includeed the init.php in a login.php and in my login.php the object/class I instantiated was error-ing. But adding the doc-blocks as you explained stopped the errors from showing. (I agree that suppressing errors is NOT the way to go)
Nice, and at the same time PhPStorm will help you with lint feedback and autocomplete
I like this solution also for the above reasons. It is working out well. Thanks @Pawel Hawro
Where should this header go? I'm confused whether it's supposed to go in the required file or the requiring file (and doesn't seem to help in either).
|
45

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

enter image description here

4 Comments

Enabling this setting could lead to a misunderstading by PHPStorm. Ex.: I have a header.php that declares a $name = 'user';. If I require or include the header file in index.php, the IDE points to another $name of a file that wasn't even included on the current script (another file of the same project). Using PHPDoc solves the problem.
Good point. Is there a way to globally specify particular variables that will always be defined elsewhere? Or even just disable inspection for variables of a particular name across all projects?
That's the solution.
TomInCode you save my life. This voice has bothered me for years. love you.
41

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
];

Comments

11

Yes it is. However, this should hint you to another issue your code most likely has (and which Phpstorm can not warn specifically about): You exploit the usage of global variables too much in addition to how you use include and require.

You can easily improve here by modularizing your code more, e.g. by making use of functions with parameters. Additionally you can consider (but you don't need to in PHP) to start writing classes and program with objects.

For PHPStorm itself, you can exclude single or multiple statements with annotations to disable the warning in certain places. However if you have many "false positives", this is cumbersome. This btw. works with any inspection warning.

See as well:

4 Comments

So what would be the actual proper way to fix this? Functions do not seem helpful in this regard at all.
@FrankyBoy: Please see the follow-up answer stackoverflow.com/a/35147818/367456 that shows how to control the inspection warnings in Phpstorm more dedicated. Also meta files may help or \@var definitions. Perhaps also defining as global variables.
both of these seem like workaround to me to just get rid of the pesky warning, without addressing the actual underlying problem/antipattern. I'm asking on alternatives for the antipattern, not how to ignore warnings.
@FrankyBoy Please describe a bit more what is the antipattern in your case, then I might be able to give suggestions in that direction. It's hard to make suggestions without knowing a bit more context.
4

By using global $db not just at the top of my function, but also atop of other files, phpstorm somehow figures out where to find the declaration and the /** @var statement of that var.

Comments

1

Personally, I'd just reduce the severity of the warning, which will then apply regardless of whether you have a prior include/require statement or not. Settings>>Editor>>Inspections>>PHP>>Undefined>>[highlight 'Undefined variable'], then adjust 'Severity' using the dropdown menu.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.