PHP buffer ob_flush() vs. flush()
Asked Answered
D

4

84

What's the difference between ob_flush() and flush() and why must I call both?

The ob_flush() reference says:

This function will send the contents of the output buffer (if any).

The flush() reference says:

Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc).

However, it continues to say:

[it] may not be able to override the buffering scheme of your web server…

So, seems to me that I could just use ob_flush() all of the time. However, I get strange results when I do that. Could someone explain in simple terms what's going on here?

Dellinger answered 16/11, 2010 at 5:10 Comment(0)
S
76

ob_flush sends an application-initiated buffer. There may be multiple nested ob_start()'s in any PHP script. ob_flush passes the current content to the upper layer.

PHP itself might (at its own discretion) buffer output. This depends on the back-end. But usually FastCGI has a socket buffer on its own. Therefore flush() needs to be invoked as well to send the current content to the web server.

And now the web server might itself implement another buffering scheme (mod_deflate or content filter), which you have no influence over. But this is seldom, as it needs to be configured specifically.

Anyway, use both.

Shandashandee answered 16/11, 2010 at 5:18 Comment(6)
Use ob_flush and flush and use them in that order.Izolaiztaccihuatl
The important detail missing from this answer is the output_buffering configuration option, whose default value in production versions of php.ini is 4096. That means that when any PHP script starts, the first 4096 bytes of output get buffered (in a buffer flushable with ob_flush()). This is why it is necessary to use ob_flush() as well as flush(). Disabling output_buffering via php.ini or calling ob_end_clean() or ob_end_flush() at the start of the script removes this necessity.Writhe
Thanks @MarkAmery, but ob_end_clean() at the "start of the script" seems counter-intuitive. You mean we should include this soon after ob_start() and before the two flushes?Gambrill
Well I have an ob_start() and an ob_end_clean() at the very start of the script. Then after any "echo" during the script, I also include ob_flush(); flush();, and I expect that while the script is taking time, whatever has been done thus far will be output on the screen (in the browser). But nothing is out put until the very end. What am I missing?Gambrill
@KhomNazid A browser observation is not the same as debugging the HTTP stream. Incremental and delayed output to an incomplete HTML DOM is not likely to be visible. Do some CLI testing with your current setup. (And then, and only then, start a different question about it.)Shandashandee
Thanks @mario. I am only looking for browser observation. Are ob_..() functions not the right ones?Gambrill
W
35

ob_flush flushes output buffers you created with a function like ob_start

flush flushes buffered output of the PHP script itself to its caller

Windsail answered 16/11, 2010 at 5:30 Comment(0)
E
26

ob_flush() is a high-level flush. It flushes high-level buffers and puts all content in the low-level, internal buffers ready to send.

  • Note that the ob_ family of functions create stacks of buffers, so just blindly writing ob_flush() everywhere is indeed going to give you "strange results" if the code was written to take advantage of this stacking.

flush() is a low-level flush, instructing PHP to flush its internal, low-level data buffers.

Below that still, there will be socket-layer buffers; below that, there are network-layer buffers. And, at the lowest level, the queue of electrons going down the data cable.

Eboni answered 21/5, 2011 at 14:39 Comment(2)
Well I have an ob_start() and an ob_end_clean() at the very start of the script. Then after any "echo" during the script, I also include ob_flush(); flush();, and I expect that while the script is taking time, whatever has been done thus far will be output on the screen (in the browser). But nothing is out put until the very end. What am I missing?Gambrill
@KhomNazid Buffering by intermediate stages, like the webserver, proxies, your browser...Eboni
P
12

I guess this is in relation to your previous question. The significant advantage of using output buffering is when it's used alongside data compression. If you're not using ob_gzhandler, there's little to gain. flush alone will just commit whatever output data is still on the server. With ob_start and its counterparts ob_flush, ob_end_clean and ob_end_flush, whatever is waiting to be compressed (look at flush and ob_flush as referring to different buckets - ob sends data to flush, flush sends data to browser - may not be accurate but that's the idea) will be wrapped up and sent to the client.

Pianette answered 16/11, 2010 at 5:49 Comment(1)
Thanks, the bucket analogy is a nice example.Dellinger

© 2022 - 2024 — McMap. All rights reserved.