Home > Backend Development > PHP Tutorial > PHP error handling mechanism

PHP error handling mechanism

高洛峰
Release: 2023-03-04 15:06:01
Original
1607 people have browsed it

In the daily project development process, there will always be some unexpected abnormal errors. If we do not deal with them relatively well, the program will look very unprofessional, and it is likely to become someone else's. Valid information for attacking the system; some error exceptions will terminate script execution. If there is no error message at this time, then we can only look at the code from the beginning. We must know that hundreds or thousands of lines of code in the project are very important to us. What a terrifying thing, so how can we quickly and accurately locate exceptions and errors during the project development process, and handle them accordingly? This article uses my own understanding of error and exception handling, and then shares it with everyone to learn from each other. Communicate and serve as a reminder.

System error handler:

PHP Under normal circumstances, errors will be output normally, but in some frameworks, it may affect the error output. It may be that the framework itself has its own processing mechanism. , or it may be processed in the code, usually these function settings:

1.error_reporting(); Set the error level of PHP and return the current level

error_reporting(report_level)

If the parameter level is not specified, the current error level will be returned. The following items are possible values ​​of level:

PHP 错误处理机制

It is worth noting here that when $level is 0, error output is turned off, that is, no errors will be output.

2.set_error_handler()

Definition and usage

The set_error_handler() function sets a user-defined error handling function.

This function is used to create the user's own error handling method during runtime.

This function will return the old error handler, or null if it fails.

Syntax

set_error_handler(error_function,error_types)

PHP 错误处理机制

Tip: If this function is used, standard PHP errors will be completely bypassed Handler function, if necessary, the user-defined error handler must terminate (die()) the script,

Note: If an error occurs before the script is executed, since the custom program has not been registered at that time, it This custom error handler will not be used.

The test code is as follows:

/**
 *
 * @param type $error_level 错误级别
 * @param type $error_message    错误信息
 * @param type $error_file 可选 错误文件
 * @param type $error_line 可选 错误行
 * @param type $error_context 可选。规定一个数组,包含了当错误发生时在用的每个变量以及它们的值。
 */
function my_error($error_level, $error_message, $error_file, $error_line, $error_context) {
  echo date('Y-m-d H:i:s') . $error_level . $error_message . $error_file . $error_line;
  var_dump($error_context);
}
set_error_handler('my_error', E_ALL);
print_r($a);
Copy after login

//From the above case, we can know that when registering the my_error method, the system will automatically overwrite the original error handling error_fuction() method

Custom Error Trigger

Definition and Usage

trigger_error() function creates a user-defined error message.

trigger_error() is used to trigger an error message under user-specified conditions. It is used with the built-in error handler or with user-defined functions created with the set_error_handler() function.

If an illegal error type is specified, this function returns false, otherwise it returns true.

Syntax

trigger_error(error_message,error_types)

PHP 错误处理机制

The test code is as follows:

/**
 *
 * @param type $level
 * @param type $msg
 */
function my_error($level, $msg) {
  switch ($level) {
  case E_USER_ERROR:
    echo "ERROR:<br/>";
    break;
  case E_USER_WARNING:
    echo "WARNING:<br/>";
    break;
  case E_USER_NOTICE:
    echo "NOTICE:<br/>";
    break;
  default:
    break;
  }
  echo "错误编号:" . $level . " <br/>";
  echo "错误信息:" . $msg;
}
//注册错误处理器
set_error_handler(&#39;my_error&#39;);
if (89 > 8) {
  //调用错误触发器
  trigger_error(&#39;这是错误啊&#39;, E_USER_WARNING);
}
Copy after login

The running results are as follows:

WARNING:
Error number: 512
Error message: This is an error

The above is the entire content of this article, I hope you all like it.

For more articles related to PHP error handling mechanism, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template