Reputation: 16411
If I set output_buffering
to on
in my .htaccess
file, are there any disadvantages.
I always recommend to my students to turn it off and to make sure that your code doesn’t output white space at the wrong time, and possibly to turn it on again just to be safe.
The documentation doesn’t really go into why you would or wouldn’t use this feature. Other SO questions discuss how to use it, but not whether it causes problems of its own. I know you can’t use this feature if PHP is run as a CGI.
Upvotes: 2
Views: 572
Reputation: 11942
Output buffering has advantages and disadvantages. The main advantage is obviously that you can buffer output without invoking response headers. The main disadvantage is that the client will wait for as long as it takes to either fill the output buffer or flush it, before receiving a response from the server. This means in the mean time the end-user is staring at a blank white screen while this happens. This also may affect certain telemetry metrics in a production environment such as TTFB (Time To First Byte) or UA rendering. Remember that the UA can actually fetch CSS, JS, and other assets in the background while waiting on the rest of the HTML from the server. This is known as stream processing. Most modern browsers implement this by processing chunks of the DOM as it streams in rather than waiting on the entire response to begin rendering the page.
This is why most people will put their CSS in the <head>
section of the HTML, for the browser to begin fetching and building the content sinks right away. In the browser the rendering engine does separate parsing on both the CSS and HTML. The HTML goes to the DOM parser and the CSS goes to the CSS parser. Further the JS goes to the JS engine, but this can causing some blocking operations on rendering (which is why most JS is usually loaded at the end of the DOM since it often needs access to the complete DOM).
This is how the browser typically renders the page in the client UA:
So sometimes shipping the response to the client as soon as possible is desirable, especially in mobile devices where rendering operations are often slower than desktop devices due to hardware limitations.
Consider the scenario where you are doing something similar to this in your code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="/your.css">
</head>
<body>
<h1>Title</h1>
<?php
$pdo = new PDO($dsn);
$data = $pdo->query(/* do complicated long-running query here */);
// This blocks further stream processing on the client-side here
// Then db result comes back and we continue printing
foreach ($data as $row) {
echo "$row[1], $row[2]...etc...";
}
?>
</body>
</html>
The partial rendering of content on the client-side is important to the user experience. Though this may not be the case for you specifically, it is generally viewed as important. So any operation that might take some time on the back end should preferably be deferred in favor of rendering as much content as possible on the client side.
Upvotes: 2