views:

37

answers:

2

Am I allowed to have two or more ob_start(); in my php files if so what is the proper way to end one ob_start(); and start another?

+2  A: 

From the manual:

Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.

In addition to stacking (nesting), you can have separate blocks in sequence.

<?
ob_start();
echo "Foo";
ob_end_flush(); // outputs buffer contents and turns off output buffering

ob_start();
echo "Bar";
ob_end_flush();
?>
bemace
A: 

You are allowed to do more than one ob_start() on a page. You end ob_start() with ob_end_clean().

ob_start();
$postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
ob_end_clean();
echo $postOutput;
matthewpavkov
If you're not doing any additional processing of `$postOutput` then `ob_end_clean()` just makes extra work compared to `ob_end_flush()`
bemace
Right, since ob_end_clean is going to erase the top buffer.
matthewpavkov