PHP error handling problems

Source: Internet
Author: User
Tags php exception handling
Php error handling is complicated. This article explains all important error-related knowledge points in php and makes it easy to understand the php error mechanism. PHP error handling mechanism

Php error handling is complicated. This article explains all important error-related knowledge points in php and makes it easy to understand the php error mechanism.

Basic knowledge

Before that, familiarize yourself with the basic knowledge of php errors.

  • Predefined Constants

  • Runtime configuration

  • Exception

  • Error handling functions

Predefined Constants

Defines all php error type constants. each constant is an integer value. its function is

The preceding value (value or symbol) is used to create a binary mask to specify the error information to be reported. You can use bitwise operators to combine these values or block certain types of errors. Note that in php. ini, only '| ','~ ','! ', ^_^ _ ^' And '&' are parsed correctly.

From the perspective of usage, there are three types:

  1. Manually throw
    E_USER_NOTICE,E_USER_WARNING,E_USER_ERROR,E_USER_DEPRECATED

  2. User-caused
    E_NOTICE,E_PARSE,E_WARNING,E_ERROR,E_COMPILE_ERROR,E_COMPILE_WARNING,E_STRICT,E_RECOVERABLE_ERROR

  3. Caused by the php kernel
    E_CORE_ERROR,E_CORE_WARNING

From the perspective of terminating program execution, there are two types:

  1. Terminate program execution
    Terminate the program and go to the error handling process.

  2. Do not terminate program execution
    Errors are generated, but the program can still be executed, and the error handling process is also entered.

For the error types in PHP, refer to this more detailed article-PHP error mechanism summary

Runtime configuration

Manual-detailed descriptions of runtime configurations, but several configurations require special attention.

  1. error_reporting
    Report error type. we recommend that you set itE_ALLAfter all types of errors are solved, configure them in the production environment.E_ALL & E_DEPRECATEDIndicates that all errors except discard errors are reported.

  2. display_errors
    Whether an error is displayed. set it to false in the production environment.error_reportingIndicates that all errors except discard errors are reported, but no error information is displayed.

  3. log_errors
    Whether the error record is enabled,Production environment must be enabledIn combination with the above two configurations, it indicates that all errors except discard errors are reported, and no error information is displayed, but the records (only php can operate on the error information) are recorded in the log.

  4. error_log
    Specified file error(Syslog is a special value). Not set by default. in the manual:

If this configuration is not set, the error message is sent to the SAPI error recorder.

Under normal circumstances, unconfigured logs are recorded in apache/nginx error logs. in combination with the above three configurations, it indicates that all errors except discarded errors are reported, and no error information is displayed, but the error information is recorded in the apache/nginx log. if the file path is configured, it indicates:Reports all errors except discard errors. no error information is displayed, but the error is recordedfile_dirLog in.

The above configurations affect the most basic performance of php errors. of course, these configurations can be implemented throughini_set()Change in code or php-fpm configuration

Error handling functions

There are not many error functions. The most important thing to note isset_error_handlerAndset_exception_handlerBecause they can be used to handle errors/exceptions.

As mentioned above, the error handling process is implemented when an error occurs. how is the error process defined?

Let's take a look at the instructions in the php Manual: Errors

Simply put,The default process is completed through configuration, but we can set a custom error handling process.

How to handle errors that terminate script execution

As mentioned above, there are two types of errors. what should I do if such errors will terminate script execution?
set_error_handlerThis error cannot be handled, which is easily ignored. Therefore, you need to find another method.

This problem is basically completed in this way (no other solutions have been seen ):

// An error that terminates the script will terminate the script execution. // The handler that has been registered through register_shutdown_function will be called. // This allows you to register our error handling process, in this way, the custom error process register_shutdown_function ('fatalerrorhandle') is entered ');... fatalErrorHandle (array $ error = null ){... if (null ===$ error) {// you can obtain the last error in this way $ error = error_get_last ();}... // log or other logic}
Exception

According to the descriptions in w3cPHP exception handling:

Exception handling is used to change the normal process of the script when a specified error (exception) occurs. This is called an exception.
When an exception is triggered, it usually occurs:

  • The current code status is saved.

  • Code execution is switched to a predefined exception processor function

  • Depending on the situation, the processor may re-execute the code from the saved code status, terminate the script execution, or continue to execute the script from the Chinese and foreign locations.

An uncaught exception terminates the script execution and produces a E-ERROR error that executes defined exception handling. if not, the default php error handling process is recorded in the log. however, exceptions and errors should be separated in programming concepts. exceptions are foreseeable, unexpected, and controllable structures for users.

As mentioned aboveset_exception_handlerIs to handle exceptions, usage andset_error_handlerConsistent. exception handling in various frameworks is very mature, generally inset_exception_handlerLieutenant GeneralExceptionTransfer to the framework's handling level. The framework also opens good interfaces for users to use, so as to achieve the purpose of user control and exception handling, and achieve customization and expansion.

Summary

Php's error handling mechanism is always ignored, but it plays a major role in debugging and monitoring errors. this article mainly introduces the main knowledge points and makes a sorting, hoping to be useful to everyone. for more details, see the manual.

Learning materials

Predefined Constants
Runtime configuration
Error handling functions
PHP error mechanism summary
Exception
Errors
PHP exception handling
Symfony Debug: it is a complete application and can be said to be a comprehensive tutorial. all error-related knowledge points are involved. read the source code.

The above is the details of PHP error handling. For more information, see other related articles in the first PHP community!

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.