How can the use of for loops in PHP be optimized for better performance?

To optimize the performance of for loops in PHP, it is recommended to minimize the number of iterations, avoid unnecessary calculations within the loop, and use more efficient loop constructs like foreach when possible. Additionally, precomputing values outside the loop and storing them in variables can help improve performance.

// Example of optimizing a for loop in PHP
$limit = 1000;
$total = 0;

// Calculate the total sum of even numbers from 1 to $limit
for ($i = 0; $i <= $limit; $i += 2) {
    $total += $i;
}

echo $total;