How to start ob_start() in parent ob_start's callback

329 Views Asked by At

I have this code

<?php
function testFunc($b){
    ob_start();
    echo "print from callback\n";
    return ob_get_clean();
}

ob_start('testFunc');
echo 'print from buffer';
ob_end_flush();

But I have a following error ob_start(): Cannot use output buffering in output buffering display handlers

I expected the result

print from callback

Please, dont suggest simplify this code, because in my codebase I have nested buffers like this

1

There are 1 best solutions below

2
Monnomcjo On

As said. "You're trying to start a output buffer inside a buffer callback. If you use this code, it will generate that error. But if you remove the ob_start() from the callback function it's OK."

And, if you expected the result to print, return the string or anything else you want to print:

function testFunc($b){
    //ob_start();
    return "print from callback\n";
    //return ob_get_clean();
}

ob_start('testFunc');
echo 'print from buffer';
ob_end_flush();