Repeated work in loop (ARCH7072)
هذا المحتوى غير متوفر بلغتك بعد.
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.
Incorrect
Section titled “Incorrect”<?phpforeach ($items as $item) { $limit = $config->get('max') * count($defaults); process($item, $limit);}Correct
Section titled “Correct”<?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.