1.2.505. 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.505.1. Suggestions

  • Move the global keyword outside the loop

1.2.505.2. Specs

Short name

Structures/GlobalOutsideLoop

Rulesets

All, Performances

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud