Zum Inhalt springen

Repeated work in loop (ARCH7072)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key php.performance.repeated-work-in-loop · Severity info · Category performance

A computation whose inputs do not depend on the loop variable is re-run on every iteration. Hoist this loop-invariant expression out of the loop.

<?php
foreach ($items as $item) {
$limit = $config->get('max') * count($defaults);
process($item, $limit);
}
<?php
$limit = $config->get('max') * count($defaults);
foreach ($items as $item) {
process($item, $limit);
}

Move computations that do not depend on the loop variable above the loop so they run once.