What are some best practices for displaying a progress indicator to users while PHP scripts are processing?

When running long PHP scripts that may take some time to process, it's important to provide users with a progress indicator to show that the script is still running and hasn't frozen. This can be achieved by using AJAX to periodically check the status of the script and update the progress bar accordingly.

// PHP script to process data
// This is just a dummy example, replace this with your actual processing logic

$totalItems = 1000;

for ($i = 1; $i <= $totalItems; $i++) {
    // Process each item
    // Simulate processing time
    usleep(10000); // Sleep for 10 milliseconds
}

// AJAX endpoint to check progress
if (isset($_GET['check_progress'])) {
    $progress = $i / $totalItems * 100;
    echo json_encode(['progress' => $progress]);
    exit;
}
```

In your HTML file, you can use JavaScript and AJAX to periodically check the progress of the PHP script and update a progress bar element on the page.

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Progress Indicator</title>
</head>
<body>
    <div id="progressBar" style="width: 0%; background-color: lightblue;">0%</div>

    <script>
        function checkProgress() {
            fetch('your_script.php?check_progress=1')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('progressBar').style.width = data.progress + '%';
                    document.getElementById('progressBar').innerText = data.progress + '%';
                    if (data.progress < 100) {
                        setTimeout(checkProgress, 1000); // Check progress every 1 second
                    } else {
                        alert('Processing complete!');
                    }
                });
        }

        checkProgress();
    </script>
</body>
</html>