What are some alternative methods to display real-time progress in PHP scripts?
When running long-running PHP scripts, it can be helpful to display real-time progress to keep users informed about the status of the process. One way to achieve this is by using AJAX requests to periodically check the status of the script and update a progress bar or message on the front end.
<?php
// Long-running PHP script
$totalIterations = 1000;
for ($i = 0; $i < $totalIterations; $i++) {
// Do some work here
// Update progress
$progress = round(($i / $totalIterations) * 100);
// Store progress in a file or database
file_put_contents('progress.txt', $progress);
}
// Display completion message
echo 'Process completed successfully.';
?>