Page 1 of 1

Output buffering not happening

Posted: Wed Jul 13, 2005 6:13 pm
by pickle
Hi all,

I'm trying to build a templating system and I know a common way of doing it is to turn on output buffering, output the template file, capture the output, then turn off buffering. I'm trying that but the output is getting through anyway. Here's some sample code:

Code: Select all

ob_start();
echo "test";
$output = ob_get_flush();

$output = "This is a ".$output;
echo $output;
The page shows: "testThis is a test". Should it not be just "This is a test"?

I've checked php.ini and there is no value for output_buffering. So, I've called:

Code: Select all

ini_set("output_buffering","on");
but nothing changes.

I know buffering is on because ob_get_flush() returns false if buffering isn't on - and it's obviously returning what it should.

Any ideas?

Posted: Wed Jul 13, 2005 6:35 pm
by Burrito
sure you are you're not doing:

Code: Select all

$output .= "This is a ".$output;
:?:

Posted: Wed Jul 13, 2005 7:03 pm
by programmermatt
ob_get_flush() flushes to the output stream, returns that output as a string and turns off output buffering.

What you want is ob_get_contents() and ob_clean()

Code: Select all

ob_start();
echo "test";
$output = ob_get_contents();
ob_clean(); 

$output = "This is a ".$output;
echo $output;

Posted: Wed Jul 13, 2005 8:19 pm
by Burrito
my next suggestion/idea that was 8O

Posted: Thu Jul 14, 2005 10:00 am
by pickle
You know, I read and read and read the documentation for ob_get_flush() and never realized it dumped the output buffer to the stream AND to the variable. Using ob_get_contents() and ob_clean() did, of course, work. Thanks a bunch.

And yes, I'm sure I wasn't concatenating.

Posted: Thu Jul 14, 2005 10:50 am
by Burrito
pickle wrote: And yes, I'm sure I wasn't concatenating.
8O