开发者

PHP turn off errors - in one file only

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 slow开发者_如何学JAVAs the code down...

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 :)


<?php

    // normal code

    // error_reporting returns the old error code
    $old_error_reporting = error_reporting(0);

    // your errorful code

    // reset error_reporting to its old value
    error_reporting($old_error_reporting);

    // normal code

Although it would be a good idea to fix what is actually causing the errors.


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.

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
}

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.


You can change the error reporting level during runtime:

<?

   error_reporting(E_ALL);

   ... some code ....

   error_reporting(0);

   ... some more code ....

   error_reporting(E_ALL);

I know of no other way but I can't think of a case where this wouldn't be sufficient. Can you?


That's really a long time ago but someone like me would maybe use my answer.

When i need to do this kind of stuff, i just put @ before the variable in order to NOT display the errors coming from this variable.

example:

switch(@$var!="e") {

....

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜