Is there any reason I shouldn't just use output buffering on everything?

Discussion in 'PHP' started by Tony Brar, Jul 11, 2013.

Thread Status:
Not open for further replies.
  1. #1
    Hi Digital Point,

    I'm thinking of putting output buffering on every page of my site, in this fashion:
    <?php
    ob_start();
    ?>
    <html>
    <head>
    <title>Example Page</title>
    </head>
    <body>
    <p>Example Content</p>
    </body>
    </html>
    <?php
    ob_end_flush();
    ?>
    PHP:
    I just wanted to know, is there any reason I shouldn't do this?
    I want to do it so I don't have header problems.
    It just seemed to me like this is one of those things that a beginning coder does stupidly. :)
    Is it?

    Thanks,
    -Tony
     
    Solved! View solution.
    Tony Brar, Jul 11, 2013 IP
  2. #2
    I guess you could, but it increases the overhead and isn't it best to debug a site and handle all the errors before it goes live rather than build in a longterm performance drag?
     
    sarahk, Jul 11, 2013 IP
    ryan_uk likes this.
  3. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #3
    You most certainly can do that. But my logical response will be "why?". PHP already sends the correct headers so the browser will know that it's HTML content. If you are so much concerned about sending the correct header type, you may as well use the following at the beginning of your scripts:
    <?php
    header("Content-Type: text/plain");
    Code (markup):
    However, that is 99.9% unnecessary on most server.

    As for the "header problems" you are referring to, I am assuming you mean notices like "Headers already sent"? These are easily avoided, once you realize the concept of headers themselves. They are supposed to be sent before ANY other content to the browser. Even sending a blank character will cause the headers to be sent prematurely (so you can't set them afterwards and you will get a PHP error if you try).

    Simply said, you need to do all your header(), setcookie() calls before you do any echo(), var_dump(), print_r().
     
    ColorWP.com, Jul 11, 2013 IP
    ryan_uk likes this.
  4. umarsa

    umarsa Well-Known Member

    Messages:
    94
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #4

    You should really be trying to AVOID using this at all times. 99% of the time there is another way to capture the data being sent to the user.

    You could of course do it but that would be very bad practice and in the long term would increase overheads and load on the server.
     
    umarsa, Jul 11, 2013 IP
  5. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #5
    You are basically putting another layer of processing power before the server, for no useful purpose. Output buffering is a useful feature in PHP, put there for a reason.
     
    ColorWP.com, Jul 11, 2013 IP
Thread Status:
Not open for further replies.