php – ob_start在发生错误时被中断

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – ob_start在发生错误时被中断脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
所以ob_start()应该捕获输出,直到调用一个缓冲函数,如ob_get_clean(),ob_get_contents(),ob_get_flush().

但是当缓冲区读取器内部抛出异常时,它会通过停止读取并回显输出而不是继续捕获它来影响读取器.这是我想要止的.

让我们说这是我的脚本:

<?PHP
    error_reporting(0);
    try {
        ob_start();
            echo "I don't wanna output this what so ever,so want to cache IT in a VARiable with using ob_ functions";
            $unlink = unlink('some file that does not exist');
            if(!$unlink) throw new Exception('Something really bad hapPEn.',E_ERROR); //throwing this exception will effect the buffer
        $output = ob_get_clean();
    } catch(Exception $e) {
        echo "<br />Some error occured: " . $e->getMessage();
        //PRint_r($e);
    }
?>

该脚本将输出

I don't wanna output this what so ever,so want to cache it in a variable with using ob_ functions
Some error occurred: Something really bad happen.

什么时候打算打印

Some error occurred: Something really bad happen.

我做错了什么,有解决方案吗?

解决方法

我的猜测是,即使在catch块内,输出缓冲仍然有效.但是,脚本以活动输出缓冲结束,因此PHP自动显示输​​出缓冲区.

因此,您可以尝试在异常处理程序中调用ob_clean().

脚本宝典总结

以上是脚本宝典为你收集整理的php – ob_start在发生错误时被中断全部内容,希望文章能够帮你解决php – ob_start在发生错误时被中断所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。