Monthly Archives: September 2016

Pushing PHP Output to the Browser

Because PHP is our favorite programming language, we do a lot of behind-the-scenes processing with it. Some of these processes are called from the command line, but most are triggered from the browser. When a long script is processing, it is useful to get updated messages about where it is at. Normally, the browser will just hang and spin until the entire script is done. But here is a way to send output to the browser before the process is finished. The loop and sleep command are contrived for demonstration purposes.

Please note that the header is very important here.


header( 'Content-type: text/html; charset=utf-8' );
for($i=1;$i<11;$i++) {
echo "Step $i complete...
";
ob_flush();
flush();
sleep(3);
}

This is dependent on your server environment. Your results may vary.