One or more compute intensive functions like count
, database calls(mysqli_query
, mysqli_fetch_assoc
, etc.) is being used in the loop condition.
Though using these functions is a cheap operation; however, there's still the function call overhead when calling it on each iteration.
It is recommended to copy the result of the function into an variable outside the loop condition.
// `count()` function is called each time the loop iterates
for ($i = 0; $i < count($array); $i++) {
echo $array[$i], PHP_EOL;
}
// `count()` function is called only once
$arrayCount = count($array);
for ($i = 0; $i < $arrayCount; $i++) {
echo $array[$i], PHP_EOL;
}