gpt4 book ai didi

php - 在 PHP 中使用 ob_start()/ob_clean/ob_flush() 进行错误处理是否可以接受?

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:10 26 4
gpt4 key购买 nike

像下面这样在 php 中进行错误处理是否可以接受?我对ob_start()/ob_clean()/ob_flush()不是很熟悉,所以我想知道使用它们有什么不好的影响吗?例如,它们会影响性能吗?

<?php 
function custorErr()
{
ob_clean();
//echo error message and some other error handling...
die();
}
set_error_handler("custorErr");
ob_start();

?>
<!doctype html>
<!-- html here -->
<html>
<head>
</head>
<body>
demo
</body>
</html>
<?php ob_flush();?>

如果这不是最佳实践,那么有没有更好的方法在出现错误时清除所有页面内容?

最佳答案

我想您的方法会非常有效,如果您想重复使用它,我认为您的方法是最好的。

另一种选择是将缓冲部分放在 try/catch block 中。如果出现问题,然后清理输出缓冲区。如果只是一个位置你想检查这个错误,我相信这种方法会更好。

<?php 
try {
ob_start();
?>
<!doctype html>
<!-- html here -->
<html>
<head>
</head>
<body>
demo
</body>
</html>
<?php
ob_flush();
}
catch {Exception $e) {
ob_end_clean(); //OR ob_clean();
echo 'error is ' . print_r($e, true);
}

一些关于清理输出缓冲的文档:

http://php.net/manual/en/function.ob-end-clean.php

http://php.net/manual/en/function.ob-clean.php

关于php - 在 PHP 中使用 ob_start()/ob_clean/ob_flush() 进行错误处理是否可以接受?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28926040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com