首页 > 解决方案 > 如果发生异常,防止 PHP 回显

问题描述

我注意到 PHPecho成功地打印字符串,即使在脚本中的某个地方抛出了错误并且没有被处理。例如:

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "started";
throw new Exception("big error");
echo "done";

打印"started",即使发生错误。状态码确实是 500,但我不认为显示部分结果适用于所有情况。

使用ob_start()andob_get_contents()提供了一些灵活性,但我希望 PHP 提供一个开关,以便在发生错误时将显示设置为无。这个开关存在吗?

标签: php

解决方案


echo "started"; // <- This will occurs
throw new Exception("big error"); // <- And here the Exception will be thrown
echo "done"; // <- therefore, this line won't be reached

推荐阅读