PHP Global error handling

Source: Internet
Author: User
Tags coding standards

Purpose of this article

PHP's global error handling, when developing projects, is useful to help developers quickly locate problems and improve productivity. By default, global errors are output directly, but a framework library used recently for development has set up global error handling, causing many error messages to be left out and time-consuming to locate the problem. Therefore, this paper studies the implementation of this library, found that it set the error_reporting and Set_error_handler, resulting in this phenomenon. Now record the usage of these two functions as a memo.

Background

PHP does not have type detection, and developers are more likely to enter incorrect words, causing fatal errors and eventually causing the script to stop executing. If you don't get any error messages at this time, it's going to be a very painful thing. You have to start debugging from the first line of the script, and print or echo in thousands of lines of code until you navigate to the wrong word. Then, there has to be a return to the original, removing all previously added print or echo. This is a dull job.

General situation

Normally, PHP will directly output the fatal error, the source of the error (file address, line number) and the reason for output, so that the development can be very convenient to locate the problem.

However, sometimes, because of the php.ini setup problem, may be a third-party framework configuration problems, resulting in this information is not output, then you must learn to set the relevant parameters, output these error messages, to help quickly locate the problem.

Error_reporting

Error_reporting is a global configuration parameter for PHP, in php.ini. Used to configure the error output level, the parameter is a bit bit, can be used to set the level of error output, the following is the information from php.ini copy:

; Error_reporting is a bit-field. Or each number up to get desired error

; Reporting level

; E_all-all errors and warnings (doesn ' t include E_strict)

; E_error-fatal Run-time Errors

; E_recoverable_error-almost Fatal Run-time Errors

; E_warning-run-time warnings (non-fatal errors)

; E_parse-compile-time PARSE Errors

; E_notice-run-time Notices (these is warnings which often result

; From a bug in your code, but it's possible that it was

; Intentional (e.g., using an uninitialized variable and

; Relying on the fact it's automatically initialized to an

; Empty string)

; E_strict-run-time notices, enable to has PHP suggest changes

; To your code which would ensure the best interoperability

; And forward compatibility of your code

; E_core_error-fatal errors that occur during PHP ' s initial startup

; E_core_warning-warnings (non-fatal errors) that occur during PHP ' s

; Initial startup

; E_compile_error-fatal Compile-time Errors

; E_compile_warning-compile-time warnings (non-fatal errors)

; e_user_error-user-generated ERROR Message

; e_user_warning-user-generated WARNING Message

; e_user_notice-user-generated NOTICE Message

;

; Examples:

;

; -Show all errors, except for notices and coding standards warnings

;

; error_reporting = E_all & ~e_notice

;

; -Show all errors, except for notices

;

; error_reporting = e_all & ~e_notice | E_strict

;

; -Show only Errors

;

; error_reporting = e_compile_error| e_recoverable_error| e_error| E_core_error

;

; -Show all errors except for notices and coding standards warnings

;

error_reporting = E_all & ~e_notice

By default, PHP will output all error messages except notice. Similarly, a function error_reporting (int $level) with the same name is provided in the PHP standard function, which is used in PHP scripts to accomplish the same function. This will not affect other programs. It is worth noting that when the $level is 0, the error output is turned off, which means that no errors are output.

Set_error_handler

The default error handling for PHP is to output the message. However, there are times when you need to define some other action, and you need to customize the error handling function. PHP provides built-in function Set_error_handler to help us register our own error-handling functions. The function prototypes are as follows:

Mixed Set_error_handler (callback $error _handler [, int $error _types = E_all | E_strict])

It is important to note that even if the error handler is registered, the default behavior will still be executed, that is, when the error occurs, the error message will still be output, so you need to set the error level to 0 in the program, and then register your own error-handling function. This is especially important in a production environment, where instant errors occur and sensitive internal error messages are not exposed to potentially malicious users. It is also important to note that custom error handlers cannot handle fatalerror (such as compilation errors). The following is an argument that uses a custom error-handling function:

12345678910111213141516171819202122232425262728293031323334353637 <?phperror_reporting(0);function error_handler ($error_level, $error_message, $file, $line) {    $EXIT= FALSE;    switch($error_level) {        caseE_NOTICE:        caseE_USER_NOTICE:            $error_type= ‘Notice‘;            break;        caseE_WARNING:        caseE_USER_WARNING:            $error_type= ‘Warning‘;            break;        caseE_ERROR:        caseE_USER_ERROR:            $error_type= ‘Fatal Error‘;            $EXIT = TRUE;            break;        default:            $error_type= ‘Unknown‘;            $EXIT = TRUE;            break;    }    printf ("%s: %s in %s on line %d\n", $error_type, $error_message, $file, $line);    if($EXIT) {       die();    }}set_error_handler (‘error_handler‘);//new NonExist();echo $novar;echo3/0;trigger_error (‘Trigger a fatal error‘, E_USER_ERROR);newNonExist();?>

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

As you can see, the last exception for "New Noexistclass ()" is not captured by the custom error handler.

Finally, incidentally, Set_exception_handler register the top layer of exception handling, in the Web one use, you can set a bit, and then a unified jump to the error handling page.

PHP Global error handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.