<?php
/**
* Flushes output for timestamps in short-lived tasks.
*
* This code is designed for situations where asynchronous processing
* is not feasible and immediate output is desired.
*/
/**
* Function to flush output.
*/
function flush_output() {
ob_flush(); // Flush output buffer.
fflush(stdout); // Flush standard output.
fflush(stderr); // Flush standard error.
}
/**
* Example usage:
*
* This function is meant to be called at key points in your task.
* After each timestamp/output you want to see immediately.
*/
function example_task() {
$timestamp = date('Y-m-d H:i:s');
echo "Task started: " . $timestamp . "\n";
flush_output(); // Flush after starting message.
// Simulate some work
usleep(500000); // 0.5 seconds
$timestamp = date('Y-m-d H:i:s');
echo "Task processing: " . $timestamp . "\n";
flush_output(); // Flush after processing message.
// Simulate more work
usleep(750000); // 0.75 seconds
$timestamp = date('Y-m-d H:i:s');
echo "Task completed: " . $timestamp . "\n";
flush_output(); // Flush after completion message.
}
// call the example task
example_task();
?>
Add your comment