PHP buffer ob_flush() vs. flush() PHP buffer ob_flush() vs. flush() php php

PHP buffer ob_flush() vs. flush()


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.


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


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.