2

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.

Manngo
  • 14,066
  • 10
  • 88
  • 110
  • Does this answer your question? [What is output buffering?](https://stackoverflow.com/questions/2832010/what-is-output-buffering) – Cypher Feb 21 '20 at 01:24
  • @Cypher No, I know what output buffering is, and I often use it manually. – Manngo Feb 21 '20 at 01:26
  • If you know what it is, then you know what the pros and cons are. – Cypher Feb 21 '20 at 01:56
  • @Cypher I disagree with your assertion here. Knowing what a thing does is not necessarily indicative of knowing all of its side effects and interactions in a real world environment. Manngo asked a perfectly legitimate question. – Sherif Feb 21 '20 at 01:58
  • 1
    @Cypher I have answered the question. Also, revenge voting is actually prohibited on SO just so you're aware. – Sherif Feb 21 '20 at 02:06
  • @Cypher Clearly you can see something I can’t. The linked question says nothing about the disadvantages of output buffering. Perhaps you can throw some light on that? Just my opinion … – Manngo Feb 21 '20 at 02:07
  • @Sherif answer below nails the question, but doesn't touch on why it is useful. Buffering the output until it is completely (or partially) rendered allows for the post processing of the data server side. This can happen in PHP (with php output buffering) or somewhere else down the line; think proxy / web filter / etc. Major side effects are latency and the memory consumed by the buffer. I have never really needed to use this for "web stuff" but I imagine it could be quite useful for multimedia (audio/video frames) being rendered as output streams. – Alex Barker Feb 21 '20 at 03:04
  • Why on earth would you be streaming audio/video through PHP? That's like loading a truck onto a plain to fly it the grocery store. – Sherif Feb 21 '20 at 03:09
  • LOL its not that slow and with FFI in 7.4+ it maybe more practical than you think. – Alex Barker Feb 21 '20 at 03:09
  • Why on earth would FFI be involved in streaming video let alone PHP? Besides the question was what are the **disadvantages** of output buffering. Not the advantages. There was already a question/answer for that. – Sherif Feb 21 '20 at 03:10
  • The foreign function interface? Because you can run native code like ffmpeg to process the data? :+1: good job dude. – Alex Barker Feb 21 '20 at 03:11

1 Answers1

1

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:

rendering

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.

Sherif
  • 11,786
  • 3
  • 32
  • 57