PHP global error handling explanation, php global explanation _ PHP Tutorial

Source: Internet
Author: User
Tags coding standards
Details about PHP global error handling, and php global explanation. PHP provides a detailed explanation of global error handling. php provides a detailed description of global error handling for PHP in this article. it is useful in development projects and can help developers quickly locate problems, improve the overall troubleshooting of PHP errors.

Purpose

PHP global error handling is useful in development projects and can help developers quickly locate problems and improve work efficiency. By default, global errors are output directly. However, a framework library used in recent development sets global error handling, resulting in many errors not being output, it takes some time to locate the problem. Therefore, after studying the implementation of this library, we find that it sets error_reporting and set_error_handler, which leads to this phenomenon. Record the usage of these two functions as a memorandum.

Background

PHP does not have a type check. it is easier for developers to enter incorrect words, causing fatal errors, and finally causing the script to stop running. If no error message is received at this time, it will be very painful. You have to start debugging from the first line of script code, print or echo continuously in thousands of lines of code until you locate the wrong word. Then, you have to go back and delete all the previously added print or echo records. This is a boring job.

General

Under normal circumstances, php will output the fatal error directly, and output the error source (file address, row number) and cause, so that the development can easily locate the problem.

However, sometimes. ini settings may be caused by a third-party framework configuration problem, resulting in no output of this information. in this case, you must learn to set relevant parameters and output these error messages to help you quickly locate the problem.

Error_reporting

Error_reporting is a global configuration parameter in php. ini. It is used to configure the error output level. the parameter is a bit and can be used to set the error output level. the following information is copied from php. ini:

; 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 are 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 have PHP suggest changes; to your code which will 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, the standard php function provides the error_reporting (int $ level) function with the same name, which is used to complete the same function in the php script. This will not affect other programs. It is worth noting that when $ level is 0, error output is disabled, that is, no error is output.

Set_error_handler

The default error handling method of php is to output messages. However, if you need to define other operations, you need to customize the error handler 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 an error handler is registered, the default behavior will still be executed, that is, when an error occurs, the error message will still be output, therefore, you need to set the error level to 0 displayed in the program, and then register your own error handler function. This method is especially important in the production environment. because of real-time errors, sensitive internal error messages are not exposed to potential malicious users. It is also important to note that custom error handling functions cannot handle fatal errors (such as compilation errors ). The following is a column using a custom error handler:

<?phperror_reporting (0);function error_handler ($error_level, $error_message, $file, $line) {  $EXIT = FALSE;  switch ($error_level) {    case E_NOTICE:    case E_USER_NOTICE:      $error_type = 'Notice';      break;    case E_WARNING:    case E_USER_WARNING:      $error_type = 'Warning';      break;    case E_ERROR:    case E_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;echo 3/0;trigger_error ('Trigger a fatal error', E_USER_ERROR);new NonExist();?>

Run this script to get the following output:

Notice: Undefined variable: novar in /your/php_demo_file.php on line 40Warning: Division by zero in /your/php_demo_file.php on line 41Fatal Error: Trigger a fatal error in /your/php_demo_file.php on line 42

We can see that the last "new NoExistClass ()" exception is not captured by the custom error handler function.

Finally, the handler mentions that set_exception_handler registers the top-layer exception handling. in the web application, you can set it and then jump to the error handling page in a unified manner.

The objective of this article is PHP's global error handling, which is useful in development projects and can help developers quickly locate some problems and improve work...

Related Article

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.