1.2.639. Local Globals¶
A global variable is used locally in a method.
Either the global keyword has been forgotten, or the local variable should be renamed in a less ambiguous manner.
Having both a global and a local variable with the same name is legit. PHP keeps the contexts separated, and it processes them independently.
However, in the mind of the coder, it is easy to mistake the local variable $x and the global variable $x. May they be given different meaning, and this is an error-prone situation.
It is recommended to keep the global variables’s name distinct from the local variables.
<?php
// This is actualy a global variable
$variable = 1;
$globalVariable = 2;
function foo() {
global $globalVariable2;
$variable = 4;
$localVariable = 3;
// This always displays 423, instead of 123
echo $variable .' ' . $globalVariable . ' ' . $localVariable;
}
?>
1.2.639.1. Connex PHP features¶
1.2.639.1.1. Suggestions¶
Add the global keyword for that variable
Change the name of the variable for another one, which is not a global variable
1.2.639.1.2. Specs¶
Short name |
Variables/LocalGlobals |
Rulesets |
|
Exakat since |
1.1.2 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Slow (1 hour) |
Precision |
High |
Available in |