PHP关闭错误 - 仅在一个文件中 [英] PHP turn off errors - in one file only

查看:103
本文介绍了PHP关闭错误 - 仅在一个文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很清楚 error_reporting(0); & ini_set('display_errors',Off); 使错误消息消失。

I am well aware about error_reporting(0); & ini_set('display_errors', "Off"); to make error messages go away.

这是一个适当的方法 - 仅针对特定文件或部分代码?
由于它显然减慢了代码的速度,因此@的似乎是一个糟糕的主意,但这个错误似乎是一个坏主意。

What would be an appropriate way to do this - for a specific file or part of code only? Surpressing errors with @'s seems like a bad idea since it apparently slows the code down...

原因是什么?我们在开发局域网中有多个memcached服务器,由于网络设置而真的不可靠,因此我们每小时都会收到错误多次,除了停止使用内存缓存或关闭整个应用程序的错误,我们无法做到这一点。 ,这将给我们头痛 - 在开发阶段的中期:)

The reason? We have a number of memcached servers in a development LAN that is really unreliable due to the network settings, thereby we are recieving errors multiple times every hour and there's nothing we can do about it except stop using memcache or turning off errors for the whole application, which would be giving us a headache - in the middle of the development stage :)

推荐答案

你有回答你的自己的问题要做一个特定的文件,error_reporting(0);将关闭错误。您也可以在脚本中多次调用它。

You've kind of answered your own question. To do it for a specific file, error_reporting(0); will turn off errors. You can also call it multiple times in a script, I think.

您还可以使用php异常通过代码块捕获错误。例如:

You can also use php exceptions to 'catch' errors over a block of code. For example:

try {
    // code to ignore errors for here
} catch {
    // you can output a custom error here, but putting nothing here will effectively mean errors are ignored for the try block
}

脚本将继续运行,通过try块,即使其中有错误。有关详细信息,请参阅 PHP手册条目

The script will continue running past the try block, even if there is an error within it. See the PHP Manual Entry for more information.

这篇关于PHP关闭错误 - 仅在一个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆