47

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?

Community
  • 1
  • 1
kxc
  • 1,357
  • 2
  • 16
  • 39
  • I think this a similar question [Undefined variable PDO][1]. I use it in PHPStorm 8. [1]: https://stackoverflow.com/questions/20036690/undefined-variable-pdo/27456071#27456071 – Curtov Dec 13 '14 at 06:09
  • So annoying, can't see anything in the side bar in historical code because it is full of annoying and uninteresting warnings. – Dgloria Aug 17 '23 at 09:47

6 Answers6

74

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.

Pawel Hawro
  • 1,291
  • 9
  • 11
  • 1
    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. – hizmarck Aug 16 '19 at 22:42
  • @Pawel Hawro This worked for me. I was getting frustrated because I instantiated an object in an init.php, then ```include```ed 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) – CodeConnoisseur Jan 02 '21 at 16:29
  • Nice, and at the same time PhPStorm will help you with lint feedback and autocomplete – fustaki Dec 30 '21 at 23:36
  • I like this solution also for the above reasons. It is working out well. Thanks @Pawel Hawro – user1794918 Jul 08 '22 at 14:49
  • 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). – Clonkex May 30 '23 at 01:47
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
];
Andreas Fröwis
  • 1,135
  • 1
  • 13
  • 18
36

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

enter image description here

TomInCode
  • 506
  • 5
  • 11
  • 2
    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. – Douglas Vicentini Dec 13 '22 at 17:48
  • 1
    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? – V1xIII Jan 10 '23 at 22:22
  • 1
    That's the solution. – Déjà vu Mar 06 '23 at 08:01
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:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • So what would be the actual proper way to fix this? Functions do not seem helpful in this regard at all. – FrankyBoy Sep 29 '20 at 16:36
  • @FrankyBoy: Please see the follow-up answer https://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. – hakre Sep 29 '20 at 21:04
  • 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 Sep 30 '20 at 08:13
  • @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. – hakre Sep 30 '20 at 15:32
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.

Puggan Se
  • 5,738
  • 2
  • 22
  • 48
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.

Geoff Kendall
  • 1,307
  • 12
  • 13