How can a for loop in PHP be optimized for speed when iterating over a large number of elements?

When iterating over a large number of elements in a for loop in PHP, it is important to optimize the loop for speed to improve performance. One way to do this is by minimizing the number of function calls within the loop and avoiding unnecessary calculations or operations. Additionally, precalculating values outside the loop that do not change can help reduce processing time.

// Example of optimizing a for loop for speed when iterating over a large number of elements
$largeArray = range(1, 1000000); // Generating a large array for demonstration

// Storing the count of elements outside the loop to avoid recalculating it
$count = count($largeArray);

// Optimized for loop with precalculated count and minimal function calls
for ($i = 0; $i < $count; $i++) {
    // Perform operations on $largeArray[$i]
}