1.2.1402. Useless Unset¶
There are situations where trying to remove a variable is actually useless.
PHP ignores any command that tries to unset a global variable, a static variable, or a blind variable from a foreach loop.
This is different from the garbage collector, which is run on its own schedule. It is also different from an explicit unset, aimed at freeing memory early : those are useful.
<?php
function foo($a) {
// unsetting arguments is useless
unset($a);
global $b;
// unsetting global variable has no effect
unset($b);
static $c;
// unsetting static variable has no effect
unset($c);
foreach($d as &$e){
// unsetting a blind variable is useless
(unset) $e;
}
// Unsetting a blind variable AFTER the loop is good.
unset($e);
}
?>
See also unset.
1.2.1402.1. Suggestions¶
Remove the unset
Set the variable to null : the effect is the same on memory, but the variable keeps its existence.
Omit unsetting variables, and wait for the end of the scope. That way, PHP free memory en mass.
1.2.1402.2. Specs¶
Short name |
Structures/UselessUnset |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Major |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Features |
unset |
ClearPHP |
|
Examples |
|
Available in |