I have read quite a bit of material on Internet where different authors suggest using output buffering. The funny thing is that most authors argument for its use only becaus
Output buffering is critical on IIS, which does no internal buffering of its own. With output buffering turned off, PHP scripts appear to run a lot slower than they do on Apache. Turn it on and they run many times faster.
The most obvious use cases are:
ob_gzhandler
or any number of filters you could devise on your own); I have done this with APIs that only support output (rather than return values) where I wanted to do subsequent parsing with a library like phpQuery.I use output buffering in order to avoid generating HTML by string concatenation, when I need to know the result of a render operation to create some output before I use the rendering.
Use output buffering to cache the data in a file, for other similar requests if you are doing a lot of database transactions and processing.
It's an old question but nobody said that an important feature of outbut buffering is filtering. It is possible to preprocess the buffer before sending it to the client.
This is a very powerful concept and opens many intriguing possibilities. In a project I used two filters simultaneously:
To enable output filtering call ob_start("callback")
where callback
is the name of the filtering function. For more details see PHP's manual for ob_start
: http://php.net/manual/en/function.ob-start.php