PHP - nested output buffering
Asked Answered
S

2

22

I have function that has :

ob_start();
//Include of some files
$content = ob_get_contents();
ob_end_clean();

Now in those includes is another function that uses the same code, and they seem to conflict. Is it possible to use them like this?

Statement answered 17/5, 2012 at 13:42 Comment(3)
How do they seem to conflict? Do you get errors?Latterly
You can't declare the same function twice, you might be able to wrap your function definition in some kind of if(!function_exists('blah')) { function blah() { ... } }Submultiple
what makes you think they conflict?Viperish
F
24

Try using output buffer like this :

ob_start();
// your includes
echo ob_get_clean();

Use this in all of your includes, and you will not get any errors

Fefeal answered 17/5, 2012 at 13:57 Comment(1)
I had similar problem but mistakenly used ob_clean() instead ob_end_clean() - be aware of their difference!Wharf
T
11

Output buffering should be stackable, you just need to match ob_start with ob_end_clean. See http://php.net/ob_start

Tew answered 17/5, 2012 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.