无法在输出缓冲显示处理程序中使用输出缓冲


Cannot use output buffering in output buffering display handlers

我重新安装了Apache,并从PHP 5.3切换到5.6。一切正常,除了我在调用ob_start():时遇到这个错误

Cannot use output buffering in output buffering display handlers

我试图在PHP中启用输出缓冲,但仍然收到以下错误:

output_buffering = 4096

您正试图在缓冲区回调中启动一个输出缓冲区。如果您使用此代码,它将生成该错误。但如果你从回调函数中删除ob_start(),那没关系

<?php
error_reporting(-1);
function callback($buffer){
    //you can't call ob_start here
    ob_start();
    return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();

尽管这是一个老问题,但我只想补充一点,当系统在缓冲时内存不足时,也会发生同样的错误。

如果是这种情况,本主题中提到的错误后面总是会出现Allowed memory size of xxx bytes exhausted错误。

可能您在输出缓冲回调中使用了缓冲函数,这在php ob_start output_recallback文档中提到是不可能的。如果不是,它应该是您使用的输出处理程序,请检查php.ini,并尽可能将其值设置为"none"。

也许这个示例代码可以帮助您:

ob_start();
echo "test";
$content = ob_get_contents();
ob_end_clean();
var_dump($content);