How can the "flush()" function be used effectively in PHP scripts to display output incrementally?

When running PHP scripts that generate a large amount of output, using the "flush()" function can be helpful to display the output incrementally instead of waiting for the entire script to finish executing. This can improve user experience by showing progress updates or partial results in real-time. To use the "flush()" function effectively, it should be combined with "ob_flush()" to flush the output buffer and send it to the browser.

<?php

// Enable output buffering
ob_start();

// Generate output incrementally
for ($i = 1; $i <= 10; $i++) {
    echo "Processing step $i...<br>";
    
    // Flush the output buffer
    flush();
    ob_flush();
    
    // Simulate processing time
    sleep(1);
}

// End output buffering
ob_end_flush();

?>