What is the purpose of using flush() in PHP and how does it affect the output to the browser?
Using flush() in PHP is used to send the output buffer to the browser immediately instead of waiting for the script to finish executing. This can be useful when you want to show progress to the user while a lengthy script is running. It helps in displaying content incrementally on the browser rather than waiting for the entire script to finish executing before displaying anything.
<?php
// Start output buffering
ob_start();
// Output some content
echo "Processing...";
// Flush the output buffer
flush();
// Continue processing
// More content to be outputted
// End output buffering and send the content to the browser
ob_end_flush();
?>