Reputation: 9180
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?
Upvotes: 30
Views: 17058
Reputation: 8459
Ok, here is the real reason : the output is not started until everything is done. Imagine an app which opens an SQL connection and doesn't close it before starting the output. What happens is your script gets a connection, starts outputting, waits for the client to get all it needs then, at the end, closes the connection. Woot, a 2s connection where a 0.3s one would be enough.
Now, if you buffer, your script connects, puts everything in a buffer, disconnects automatically at the end, then starts sending your generated content to the client.
Upvotes: 9
Reputation: 6264
Serious web applications need output buffering in one specific situation:
Your application wants control over what is output by some 3rd-party code, but there is no API to control what that code emits.
In that scenario, you can call
ob_start()
just before handing control to that code, mess around with what is written (ideally with the callback, or by examining the buffer contents if you must), and then callingob_flush()
.
Ultimately, PHPs' ob_functions are a mechanism for capturing what some other bit of code does into a buffer you can mess with.
If you don't need to inspect or modify what is written to the buffer, there is nothing gained by using ob_start()
.
Quite likely, your 'serious application' is in fact a framework of some kind.
You don't need ob_start()
in order to make use of output buffering. Your web-server already does buffer your output.
Using ob_start()
does not get you better output buffering - it could in fact increase your application's memory usage and latency by 'hoarding' data which the web-server would otherwise have sent to the client already.
ob_start()
...In some cases, you may want control over when the web-server flushes its buffer, based on some criteria which your application knows best. Most of the time, you know that you just finished writing a logical 'unit' which the client can make use of, and you're telling the web-server to flush now and not wait for the output buffer to fill up. To do this, it is simply necessary to emit your output as normal, and punctuate it with flush()
.
More rarely, you will want to withhold data from the web-server until you have enough data to send. No point interrupting the client with half of the news, especially if the rest of the news will take some time to become available. A simple ob_start
later concluded by an ob_end_flush()
may indeed be the simplest and appropriate thing to do.
If your application is taking responsibility for calculating headers which can only be determined after the full response is available, then it may be acceptable.
However, even here, if you can't do any better than deriving the header by inspecting the complete output buffer, you might as well let the web-server do it (if it will). The web-server's code, is written, tested, and compiled - you are unlikely to improve on it.
For example, it would only be useful to set the Content-Length
header if your application knows the length of the response body after before it computes the response body.
You should not ob_start()
to avoid the disciplines of:
If you do these, they will cause technical debt which will make you cry one day.
Upvotes: 13
Reputation: 28797
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
Upvotes: 3
Reputation: 279
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.
Upvotes: 0
Reputation: 63529
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.Upvotes: 5
Reputation: 4673
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.
Upvotes: 2
Reputation: 94284
If you want to output a report to the screen but also send it through email, output buffering lets you not have to repeat the processing to output your report twice.
Upvotes: 7
Reputation: 10582
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.
Upvotes: 3
Reputation: 20073
It's useful if you're trying to display a progress bar during a page that takes some time to process. Since PHP code isn't multi-threaded, you can't do this if the processing is hung up doing 1 function.
Upvotes: 0
Reputation: 6258
i use output buffering for one reason ... it allows me to send a "location" header after i've begun processing the request.
Upvotes: 3
Reputation: 22841
We used to use it back in the day for pages with enormously long tables filled with data from a database. You'd flush the buffer every x rows so the user knew the page was actually working. Then someone heard about usability and pages like that got paging and search.
Upvotes: 1