How can a PHP foreach loop be used to build a progress bar that increments with each iteration?

To build a progress bar that increments with each iteration using a PHP foreach loop, you can calculate the percentage of completion for each iteration and update the progress bar accordingly. You can achieve this by determining the total number of iterations beforehand and then calculating the percentage completed for each iteration based on the current iteration number.

$totalIterations = 10; // Total number of iterations
$progress = 0; // Initial progress value

foreach ($items as $item) {
    // Perform tasks here
    
    // Calculate percentage completion
    $progress = ($progress + 1) * 100 / $totalIterations;
    
    // Update progress bar
    echo '<div style="width: 100px; border: 1px solid #000;">';
    echo '<div style="width: ' . $progress . '%; background-color: #00ff00;"> </div>';
    echo '</div>';
}