views:

116

answers:

5

This is purely for learning more about output buffering and nothing more. What I wish to do is echo a string to the browser, sleep 10 seconds, and then echo something else. Normally the browser would wait the full 10 seconds and then post the whole result, how I would I stop that? An example:

ob_start();
echo "one";
sleep(10);
echo "two";
A: 

With ob_flush() - but that will clear the buffer contents. You can't inject a delay into a buffer, it just doesn't work like that.

You either output the entire buffer at once, or hold on to the entire buffer for later use.

Peter Bailey
this is not true: http://php.net/ob_flush - "This function will send the contents of the output buffer (if any)."
pilif
Yes - AND "as the buffer contents are discarded after ob_flush() is called. " I never said it *only* cleared the buffer contents.
Peter Bailey
this just means that echo "a"; ob_flush(); echo "b" will output "ab" and not "aab". ob_flush() sends out the data that has accumulated in PHP's buffer and then cleans that buffer. But it is sent out.
pilif
Um. Right. We're not in disagreement. So what's your point?
Peter Bailey
A: 
Svisstack
+1  A: 
ob_start();
echo "one";
ob_flush();
sleep(10);
ob_start();
echo "two";

Is that what you mean?

faileN
@faileN: This still waits until the end to output anything.
John
try adding this to the beginning of your script: ob_implicit_flush(true);
Ollie
Did not work, but I got it to work anyway (see comment on pilif's answer), Thanks
John
A: 

If I understand correctly, you are trying to print part of the response on screen, wait 10 seconds and output the rest, all this when the page is loading. This would require some client side scripting for that as PHP will output the entire response at the end.

I think a combination of ob_flush and flush might work, but buffering is not handled the same on every browser (such as IE).

Steven Rosato
+1  A: 

faileN's answer is correct in theory. Without the ob_flush() the data would stay in PHP's buffer and not arrive at the browser until the buffer is implicitly flushed at the end of the request.

The reason why it still doesn't work is because the browsers also contain buffers. The data is now sent out correctly, but the browser waits after getting "one" before it actually kicks off rendering. Otherwise, with slow connections, page rendering would be really, really slow.

The workaround (to illustrate that it's working correctly) is, of course, to send a lot of data at once (maybe some huge html comment or something) or to use a tool like curl on the command line.

If you want to use this sending/sleeping cycle for some status update UI on the client, you'd have to find another way (like long-polling and AJAX)

pilif
I just used a str_repeat of a blank space until 256 bytes, read this would work, and it makes it flawless. Although I need to send \r\n's along with the content before I flush it out for it to work. Thanks for the idea of sending large content.
John