Undefined Variable/Show Usages if set in IF statements?

I'm all for learning proper coding standards, and I clearly must be doing things unconventionally, but if I set a variable inside an if statement, in fileA.php and then require fileA.php and echo $setVariable, I get undefined variable. If I move the $setVariable outside of the if statement, it works fine.

 

I guess (might be asking TOO much), that Show Usages literally knows everywhere you've called $setVariable and it would show... all usages.

 

Use-case:

init.php - Check if session is set, and sets additional variables if so.

home.php - Requires init.php, and then further down echo $setVariableA shows undefined, AND Show Usages doesn't show $setVariableA = //things in init.php. It's like PhpStorm literally knows nothing about the $setVariableA in init.php if it's set inside the IF statement. Now, I can see an argument for making null variables set outside of the if statement, but that seems even more improper just so I can get PhpStorm to "work" per se.

 

Am I doing something wrong?

0
2 comments

Could you please attach files/code parts you're working with to avoid any misunderstanding?

0
Avatar
Permanently deleted user

config.php

if ($_SESSION) {

$var = "set";

}

 

index.php

require_once 'config.php'

if (isset($clientCompanyName)) {

echo $clientCompanyName;

}

 

Ctrl-Click on $clientCompanyName says the isset is the only usage.

 

If I do:

 

config.php

$var = "";
if ($_SESSION) {

$var = "set";

}

 

index.php

require_once 'config.php'

if (isset($clientCompanyName)) {

echo $clientCompanyName;

}

 

Then Ctrl-Click takes me to the empty variable in config.php.

 

So it looks like PhpStorm doesn't read variables inside IF conditions? I somewhat get that, but it knows what's in all files of the project, it still should know that $clientCompanyName is contained in config.php.

 

 

 

0

Please sign in to leave a comment.