I have read quite a bit of material on Internet where different authors suggest using output buffering. An interesting thing is that most authors argument for its use only because it allows for mixing response headers with actual content.
I think that Web applications crossing certain size / complexity threshold should not mix generating headers and content, and that the developer should rightfully suspect potential faults in applications that attempt to send headers after body has been generated.
This is my first argument against the ob_*
output buffering API. Even for that little convenience you get - mixing headers with output - I believe it isn't worth it, unless one simply is rapidly "hacking" or "prototyping" scripts.
Also, I think most people dealing with the output buffering API do not think about the fact that even without the explicit output buffering enabled, PHP in combination with the Web server it is plugged into, still does some internal buffering anyway. It is easy to check - have a script echo some short string, then sleep for say 10 seconds, then do another echo. Go to the script URL with a Web browser and watch the blank page pause for 10 seconds, with both lines appearing thereafter. Before some say that it is a rendering artefact, not traffic, tracing the actual traffic between the client and the server shows that the server has generated the Content-Length
header with an appropriate value for the entire output - suggesting that the output was not sent progressively with each echo
call, but accumulated in some buffer and then sent on script termination.
This is one of my gripes with explicit output buffering - why do we need two different output buffer implementations on top of one another? May it be because some of it is subject to conditions a PHP developer cannot control, so another means to control it is put into PHP?
In any case, I for one, start to think one should avoid explicit output buffering (the series of ob_*
functions) and rely on the implicit one, assisting it with the good flush
function, when necessary. Maybe if there was some guarantee from the Web server to actually commit output to the client with each echo/print call, then it would be useful to set up explicit buffering - after all one does not want to send response to the client with some 100 byte chunks. But the alternative with two buffers seems like a somewhat useless layer of abstraction.
With all this in mind, why use output buffering?