tags:

views:

82

answers:

3

Further to my previous question, what's the best approach when I want to buffer PHP output until I have performed all processing? I want to buffer to leave myself the option to to redirect to an error page, which I can't do after any output.

So, what's the best practise? Use a variable $output and keep appending to it, then output it at the end? Or use ob_str(), etc?

Is there a peformance to choose code-maintainability reason for one over the other? Or is it just personal preferance?

+2  A: 

For me, I did this:

<?php

ob_start();

//do your process here

if($error)
{
  ob_end_clean();
  header('Location: /some/path.php');
  exit;
}
ob_end_flush();

?>
silent
+1 That seems to be the way to go. Thanks.
LeonixSolutions
+2  A: 

I open a buffer with ob_start(); ( http://php.net/manual/en/function.ob-start.php )

Then anything that would normally be sent to the browser (except headers) is stored in the buffer until I close it. When I want to output or manipulate the buffer, I access it like this:

$buffer = ob_get_clean(); ( http://php.net/manual/en/function.ob-get-clean.php )

There are lots of other buffer options here:

http://www.php.net/manual/en/ref.outcontrol.php

This is the best way in my opinion because you don't have to keep adding items to the buffer; PHP is automatically capturing everything as long as the buffer is open.

Casey
+1 I tend to agree and wil probably do it that way. I was just looking for confirmation (or contradiction)
LeonixSolutions
A: 

Well written code needs no output buffering. By that I mean: first, you do all your processing, without any output. Business logic, validation, database access - this kind of stuff. After this is done, you can close the DB connection, the session, etc. because all you do is create your output based on data collected above.
This method usually results in far better maintainable code.

Maerlyn
+1 for talking the time to answer. I tend to agree with you, up to a point ... when all the processing is done up front the code has to rememebr a lot of information until all processing is over, and that either means a slew of not so maintainable variables - or buffering. So, which is it? One big variable for the whole page, or output buffer (or several dozen variables)?
LeonixSolutions
I'd rather have several dozen variables and good separation than spahetti code. MVC frameworks help a lot.
Maerlyn
Output buffering is useful in some scenarios, that's why it exists in PHP.
Casey
+1 to both. Maerlyn, I think (hope) we are saying the same thing in differnt ways. I don't see that ouput buffering leads to spaghetti code, though. And even MVC can lead to errors as you are building that output page. So it just seems to boild down to building the page in a bunch of variables, in a single string or in PHP's output buffer.
LeonixSolutions
@Casey It may be useful in some situations, but I didn't find any of those in the last ~2 years. Still, I agree: it's there for a reason. @Leonix I've seen quite a few codes that use output buffering like you do, ultimately most of them evolved into spaghetti - but not all of them, so there's still hope. For errors, there's try-catch. And I can't stress the usefullness of the frameworks enough.
Maerlyn