1.2.507. Global Inside Loop¶
The global and static keywords must be used outside loops. Otherwise, they are evaluated at each loop, slowing the whole process. This is a micro-optimisation.
<?php
// Here, global is used once
global $total;
foreach($a as $b) {
$total += $b;
}
// Global is called each time : this is slow.
foreach($a as $b) {
global $total;
$total += $b;
}
?>
1.2.507.1. Suggestions¶
Move the global keyword outside the loop
1.2.507.2. Specs¶
Short name |
Structures/GlobalOutsideLoop |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Very high |
Available in |