What are the advantages of pre-calculating the maximum index value in a loop instead of calling count() repeatedly in PHP?

When working with loops in PHP, it is more efficient to pre-calculate the maximum index value outside of the loop rather than calling count() repeatedly inside the loop. Calling count() within the loop can lead to unnecessary overhead as the count of the array is recalculated each time the loop iterates. By pre-calculating the maximum index value, you can improve the performance of your code.

// Pre-calculate the maximum index value
$maxIndex = count($array);

// Loop through the array using the pre-calculated maximum index value
for ($i = 0; $i < $maxIndex; $i++) {
    // Your loop logic here
}