PHP error management statements

Lecture



PHP supports one error control statement: the @ sign. If it precedes any expression in the PHP code, any error messages generated by this expression will be ignored.

In case the option track_errors is set , all generated error messages will be stored in the $ php_errormsg variable. This variable will be overwritten whenever each new error occurs, so if necessary, check it immediately.

// Преднамеренная ошибка при работе с файлами
$my_file = @file ('non_existent_file') or
die ("Failed opening file: error was '$php_errormsg'");

// работает для любых выражений, а не только для функций
$value = @$cache[$key];
// В случае если ключа $key нет, сообщение об ошибке не будет отображено

?>

Attention : The @ operator works only with expressions. There is a simple rule: if an arbitrary language construct returns a value, then you can use the preceding @ operator. For example, you can use @ before a variable name, an arbitrary function or a call to include () , a constant, and so on. At the same time, you cannot use this operator before defining a function or class, conditional constructions such as if or foreach .

Attention : The @ operator does not suppress the output of errors that occur at the stage of parsing the script.

Today, the @ operator suppresses the output of messages even about critical errors interrupting the operation of the script. Among other things, this means that if you used @ to suppress errors that occur during the operation of a function, if it is not available or written incorrectly, the further work of the script will be stopped without any notification.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)