Tuesday, August 9, 2011

php error suppression

It is quite often that we can see error suppression operator @ is wildly used in legacy PHP application. For example:

@getimagesize($image);
@file($file);
@mysql_pconnect();
@mysql_query();
...

Let's see what PHP will do in the background when error suppression operator @ is used.

PHP will actually translate a simple statement @file($file) into three statements:

$saveOldSetting = error_reporting(0);
file($file);
error_reporting($saveOldSetting);

error_reporting(0) will trun off all error reporting and return the old error report setting value. Then PHP will execute file($file) as normal. After that, PHP will restore the error report setting to the old value.

We know what it does now. To use it or not, it is up to developer's judge. Personally, i believe every error should be handled decently so i won't use it in my code.

No comments: