tags:

views:

148

answers:

3

I have a php script that connects 10 different servers to get data. I want it to print the results of the 1st connection before the second one begins.

+8  A: 

Using flush and/or ob_flush, you should get what you want.

Here is a quick demonstration :

for ($i=0 ; $i<10 ; $i++) {
    echo "$i<br />";
    ob_flush();
    flush();
    sleep(1);
}

Each second, a number will be sent to the browser, without waiting for the loop/script to end.
(Without both flush and ob_flush, it waits until the end of the script to send the output)


Explanation about why you need both, quoting from the flush page in the manual :

Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats.

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.


If this doesn't work for you, taking a look at the comments on the two pages of the manual can give you a couple of pointers on "why it could fail"

Pascal MARTIN
I had no idea...
sshow
that is definetely what i want, thank you
melih
@sshow : I've heard this question asked a couple of times by colleagues, at work, actually -- and used it myself once or twice -- that helps ^^
Pascal MARTIN
@melih : you're welcome :-) Have fun !
Pascal MARTIN
+1  A: 

ob_end_flush http://us.php.net/ob%5Fend%5Fflush

This function empties the output buffer and disables output buffering. Everything after this function is send to the browser immediately.

Will Bickford
A: 

Yeah, ob_flush should do it. I do this all the time with a LOOONG page, when I want to watch the progress of the operation.

Cristi Cotovan