2

Very quick question about programming practices here:

I've always used echo() to output HTML code to the user as soon as it was generated, and used ob_start() at the same time to be able to output headers later in the code. Recently, I was made aware that this is bad programming practice and I should be saving HTML output until the end.

Is there a reason for this? What is it, and why isn't output buffering a good alternative?

Thanks!

user509006
  • 107
  • 2
  • 9

3 Answers3

2

Some thoughts, not necessarily in order.

  • Using echo to output HTML is messy. PHP is a template language, you can break out of it if you need to output HTML:

    <?php echo "<div id=\"foo\">" . $bar . "</div>"; ?>
    

    vs

    <div id="foo"><?php echo $bar; ?></div>
    
  • Producing HTML first and outputting headers later is messy logic. Decide what you want to send first, then send appropriate headers, then produce the content.
  • Buffering HTML to send headers later itself is not really a problem, but it's an indicator of badly structured flow.
  • Your apps could likely benefit from some compartmentalization/breaking down of logical steps.

Look into the MVC pattern.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • I think I understand your logical steps point, but I am not sure if it helps in some cases. Bear with me, as I am not a professional programmer but am always looking for ways to optimize my code. Say I am using it to check if a user is logged in or not, and then redirect them back to the login page if they are not. Would it be beneficial for me to do login checks, one at the start to redirect and one to display the content or should I use output buffering and just do one when I begin to display the content? – user509006 Jan 05 '11 at 03:32
1

Whenever any HTML is sent to the browser, headers are received/created. PHP can't send any more headers after this happens. So, by sending code "early", you're disabling PHP's ability to send headers, and limiting the flexibility of your code (either now or for future changes).

Ben
  • 54,723
  • 49
  • 178
  • 224
  • Using `ob_start()` allows you to capture all output in a buffer. – alex Jan 05 '11 at 03:24
  • I get around this using output buffering. Why is that considered bad practice? – user509006 Jan 05 '11 at 03:27
  • @alex - I'm addressing "I should be saving HTML output until the end". – Ben Jan 05 '11 at 03:27
  • @alex - also aren't there 2 types of buffers, the PHP buffer, and the browser buffer? And apparently they're flushed differently according to browser: http://stackoverflow.com/questions/4191385/php-buffer-ob-flush-vs-flush – Ben Jan 05 '11 at 03:28
  • @user - if you're referring to early HTML output, then it's not recommended basically because you can't "change your mind" later in your design. – Ben Jan 05 '11 at 03:38
1

It is good to handle all sorts of things before you output in a view - for example, you may need to send additional headers such as Location and Set-Cookie.

Also, you never know what kind of view you will need - this response this time is HTML, but what if you want it as JSON or XML later? You'll have a difficult time restructuring.

If you had left all output to a final view, you could swap the HTML for an XML template.

alex
  • 479,566
  • 201
  • 878
  • 984