Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


And now the webserver 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.

PHP itself might (at its own discretion) buffer output. This depends on the backend. 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 webserver.

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.

Use ob_flush and flush and use them in that order.

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

Note
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


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

ob_flush flushes output buffers you created with a function like ob_start

Note
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


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.

Is the analogy backwards? I'd write "ob sends data to flush; flush sends data to browser"

Thanks, the bucket analogy is a nice example.

Note
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

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.

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.

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.

Use ob_flush and flush and use them in that order.

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.

Note
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


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.

Thanks, the bucket analogy is a nice example.

Note
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


  • 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.

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.

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

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
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


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

ob_flush flushes output buffers you created with a function like ob_start

Note
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


  • 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.

@Brett: Well, technically there's no "queue" of electrons "going down the data cable" :P

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.

I know... the +1 was for the good humor :D

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

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
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


  • 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.

@Brett: Well, technically there's no "queue" of electrons "going down the data cable" :P

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.

I know... the +1 was for the good humor :D

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

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
Rectangle 27 0

PHP buffer ob_flush() vs. flush()?


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.

Is the analogy backwards? I'd write "ob sends data to flush; flush sends data to browser"

Thanks, the bucket analogy is a nice example.

Note