Counting array
PHP Function count() is fast but when used in a loop this changes really severely. Calculating the size of an array on each iteration of a loop, if the array or collection contains a lot of items, will result in much longer execution time. Counting size should be done outside the loop not on each iteration.
Wrong:
for ($i = 0; $i < count($array); $i++){
// do something
}
Good:
$size = count($array);
for ($i = 0; $i < $size; $i++){
// do something
}
Comments
Post a Comment