Do not call a function in the declaration of a for loop to avoid the function being called each time the loop is iterated.
Instead of:
for ($i = 0; $i < count($array); $i++) {}
write:
$count = count($array); for ($i = 0; $i < $count; $i++) {}