PHP custom error handling

小云云
Release: 2023-03-17 15:08:01
Original
2049 people have browsed it

Normally, PHP will directly output the fatal error, and output the source of the error (file address, line number) and reason, etc., so that developers can easily locate the problem. But sometimes, this information may not be output due to php.ini settings or third-party framework configuration issues. At this time, you must learn to set relevant parameters yourself and output these error messages to help quickly locate the problem.

error_reporting is a php global configuration parameter in php.ini. Used to configure the error output level and can be used to set the level of error output.

error_reporting(int $level), when $level is 0, error output is turned off, that is, no errors will be output.

set_error_handler

php's default error handling is to output the message. However, sometimes you need to define some other operations, in which case you need to customize the error handling function.

php provides the built-in function set_error_handler to help us register our own error handling functions. The function prototype is as follows:

mixed set_error_handler ( callback $error_handler [, int $error_types = E_ALL | E_STRICT ] )

It is worth noting that even if the error handling function is registered, the default behavior will still be When executing, that is, when an error occurs, the error message will still be output, so you need to set the error level to 0 explicitly in the program, and then register your own error handling function. This approach is especially important in a production environment because even if something goes wrong, sensitive internal error information will not be exposed to potentially malicious users. It is also important to point out that custom error handling functions cannot handle fatal errors (such as compilation errors).

The following is an example of using a custom error handling function:


        
Copy after login

echo $novar;
echo 3 / 0;
trigger_error('Trigger a fatal error', E_USER_ERROR );
new NonExist();

Execute this script to get the following output:

Notice: Undefined variable: novar in /your/php_demo_file.php on line 40

Warning: Division by zero in /your/php_demo_file.php on line 41

Fatal Error: Trigger a fatal error in /your/php_demo_file.php on line 42

can be seen , the last "new NoExistClass()" exception was not caught by the custom error handling function.

Finally, by the way, set_exception_handler registers top-level exception handling. In web applications, you can set it and then jump to the error handling page uniformly.

The above is a case of PHP custom error handling, I hope it can help everyone.

Related recommendations:

A case of php custom function implementing array comparison function

php custom/system/class constant code example Detailed explanation

php custom/system/class constant code example detailed explanation

The above is the detailed content of PHP custom error handling. For more information, please follow other related articles on 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