Output buffering not happening

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Output buffering not happening

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

sure you are you're not doing:

Code: Select all

$output .= "This is a ".$output;
:?:
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post 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;
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

my next suggestion/idea that was 8O
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

pickle wrote: And yes, I'm sure I wasn't concatenating.
8O
Post Reply