views:

90

answers:

4

I thought flush(); would work, at least from what Google/Stackoverflow tell me, but on my Windows WAMP (Windows, Apache, MySQL, PHP) system it doesn't work.

Is there some PHP setting I have to set to make flush() work?

Here's my code:

<?php
echo "Fun";

flush();

sleep(5);

echo "<br>Mo";
?>

The code just outputs all together when the script is done executing (after 5 seconds).. I don't want this, I want 'Fun' to show up right away, and then after 5 seconds 'Mo'.

I've tried other combinations of flush like ob_end_flush(); or ob_implicit_flush(true); but nothing is working. Any ideas?

A: 

Check your php.ini for output_buffering.

Yorirou
output_buffering = On <-- that's what i have.
Luca Matteis
set it to Off, it may solve your problem
Yorirou
A: 

Maybe the problem is Apache here, which also may have buffers...

Savageman
+2  A: 

The script works fine from CLI, displaying "Fun", waiting 5 secs before displaying "<br>Mo".

For a browser the results might be a bit different because:

  1. The browser wont start rendering right away. Getting 3 bytes of data for HTML document isn't enough to do anything, so it'll most likely wait for a few more.
  2. Implicit IO buffering on the lib level will most likely be active until a newline is received.

To work around 1) use text/plain content type for your test; 2) needs newlines, so do an echo "Fun\n"; and echo "<br>Mo\n"; Of course you wont be using text/plain for real HTML data.

jmz
Yeah, I like your answer. Also can be client-side. :-)
Savageman
You're right... I've been using Chrome all this time and it wasn't working. Trying my initial example (even without newlines and text/plan output) on Firefox worked great... I wonder if there's a way to make this work cross-browser... +1 but you might want to take out the \n and text/plain stuff.
Luca Matteis
A: 

If you're using CGI/FastCGI, forget it! These don't implement flush. The Webserver might have it's own buffer.

You can disable all output buffering in PHP with following command:

ob_implicit_flush();
Vincent