Hudzilla.org - the homepage of Paul Hudson
Contents > Output Buffering Wish List | Report Bug | About Me ]

13.9     Flushing output: flush()

This is NOT the latest copy of this book; click here for the latest version.

void flush ( void )

There is a special function in PHP called flush(), which is not really related to output buffering because it works with standard output, however it is so very similar to what we've just been looking at that it makes sense to cover it here.

Flush() sends all output out immediately, without waiting for the end of the script, and you can call it as often as you want. Calling flush() has the effect of making the browser update with new content. Take a look at this example script:

Author's Note: Internet Explorer has an "optimisation" that makes it only render a page after it has received the first 256 bytes whether or not you use flush() - you might find these example scripts do not work as described in IE. That is not to say the concept is wrong - merely that there is not enough room here to demonstrate a longer example! To make the scripts work, make them output at least 256 characters before the first call flush() - in your own scripts, this will not be a problem.

<HTML>
<BODY>
This page is loading...<BR />
<?php sleep(2); ?>
Almost there...<BR />
<?php sleep(2); ?>
Done.<BR />
</BODY>
</HTML>

If you try that you will see the page appears all at once, having taking a little over four seconds to load - not a very helpful progress monitor! Now consider the following script, making use of flush():

<HTML>
<BODY>
This page is loading.<BR />
<?php flush(); sleep(2); ?>
Almost there...<BR />
<?php flush(); sleep(2); ?>
Done.<BR />
</BODY>
</HTML>

This time you will literally see the page loading - each line will appear one by one, as seen in these screenshots:







Now take you know it is possible to construct a page piece by piece, take a look at this bit of code - JavaScript is now used to alter what is there already, which makes for a much nicer-looking progress meter.

<HTML>
<BODY>
<DIV ID="flushme">
Hello, world!
</DIV>
<?php flush(); sleep(2); ?>
<SCRIPT>
d = document.getElementById("flushme");
d.innerHTML = "Goodbye, Perl!";
</SCRIPT>
<?php flush(); sleep(2); ?>
<SCRIPT>
d.innerHTML = "Goodnight, New York!";
</SCRIPT>
</BODY>
</HTML>

The JavaScript in there locates the DIV HTML element on the page, then sets its innerHTML property (the contents of the DIV) to different messages as the script loads - a simple, yet effective way to handle keeping users up to date while a script loads.

Using flush() is good for all sorts of things, but as you have seen it is particularly good when you are executing a long script and want to keep users informed. It takes very little work to print out "Please wait - generating your file" and calling flush() before creating a 500MB file - you can even follow up with printing out "File created - click here to download", so that your scripts feel much more interactive.





<< 13.8 Other OB functions: ob_get_length(), ob_get_level(), and ob_list_handlers()   13.10 Compressing output >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 05 Sep 2008

Very spooky as I have just been looking for a way to update the browser whilst a script is in a While loop!

^Lestat - 05 Sep 2008

"Re IE and tables: you can't directly. IE needs the </table> tag in order to render a table. It makes no difference whether you flush the output before </table> because IE will wait for </table> before rendering anything.

The only way to build a table a row at a time is to output each row as a table. For that you'll need to fix the width of both the table and each column so that it looks like one coherent whole when the user sees it."

Outstanding. I've been looking for this. It seems heavy to do table coding, so instead Im using <ul>'s and a bit o css to emulate a table.

Thanks!

Wolfgang - 05 Sep 2008

On my server it doesn't work. After 4 Seconds i get the page. I have already changed the output_buffering in my php.ini to off or to 1. Butt still no changes. Do I have to change something on my httpd.conf??

Another PHP User - 05 Sep 2008

Well well: four visitors in no time at all!

Re IE and tables: you can't directly. IE needs the </table> tag in order to render a table. It makes no difference whether you flush the output before </table> because IE will wait for </table> before rendering anything.

The only way to build a table a row at a time is to output each row as a table. For that you'll need to fix the width of both the table and each column so that it looks like one coherent whole when the user sees it.

Firefox is different and can render rows in tables, adjusting everything as it goes along.

A PHP User - 05 Sep 2008

To Philippe - spooky, as I'm looking for exact same thing and have just happened upon this page via google. Any ideas appreciated! Regards, Bryn.

Philippe@faure.ca - 05 Sep 2008

I am looking for a way to trick IE to start to render a table using PHP. There is lots of processing to be completed, and once a line of the table if available, I would like for IE to display it. I was wondering if someone has come across this and found a solution? FF seems to work properly.

cwluv2001 - 05 Sep 2008

tes tes 123



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow