


Detailed explanation of PHP's global error handling_php skills
Purpose of this article
PHP’s global error handling is very useful when developing projects. It can help developers quickly locate some problems and improve work efficiency. By default, global errors will be output directly, but a framework library recently used during development has set global error handling, resulting in many error messages not being output, and it is time-consuming to locate problems. Therefore, I studied the implementation of this library and found that it sets error_reporting and set_error_handler, causing this phenomenon. Now record the usage of these two functions as a memo.
Background
PHP does not have type detection. It is easier for developers to enter wrong words, causing fatal errors and eventually causing the script to stop executing. If you don't get any error message at this time, it will be a very painful thing. You have to start debugging from the first line of code in the script, and continue to print or echo through thousands of lines of code until you locate the misspelled word. Then, you have to go back and delete all the previously added prints or echoes. This is an extremely boring job.
General conditions
Under normal circumstances, PHP will output fatal errors directly, and output the source of the error (file address, line number) and reason, etc. In this way, 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 question.
error_reporting
error_reporting is a php global configuration parameter in php.ini. Used to configure the error output level. The parameter is a bit, which can be used to set the error output level. The following is the information 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 PHP standard function provides the function error_reporting(int $level) with the same name, which is used to complete the same function in PHP scripts. This will not affect other programs. It is worth noting that 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 executed, that is, when an error occurs, the error message will still be output, so the error level needs to be set to 0 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). Here is an example of using a custom error handling function:
<?php error_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(); ?>
Executing this script will 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 final "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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is a widely used server-side programming language that provides powerful and dynamic capabilities to websites. However, in practice, developers may encounter a wide variety of errors and exceptions. One of the common errors is PHPFatalerror:Uncaughtexception'Exception'. In this article, we will explore the causes of this error and how to fix it. The concept of exception In PHP, exception refers to the unexpected situation encountered during the running process of the program, resulting in

PHP exception handling tips: How to use try...catch blocks to catch and handle multiple exceptions Introduction: In PHP application development, exception handling is a very important part. When an error or exception occurs in the code, reasonable exception handling can improve the robustness and reliability of the program. This article will introduce how to use the try...catch block to capture and handle multiple exceptions, helping developers perform more flexible and efficient exception handling. Introduction to Exception HandlingExceptions refer to errors or special situations that occur when a program is running. When an exception occurs

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

When writing PHP code, exception handling is an integral part of making the code more robust and maintainable. However, exception handling also needs to be used with caution, otherwise it may cause more problems. In this article, I will share some best practices for exception classification in PHP programs to help you make better use of exception handling to improve code quality. The concept of exception In PHP, exception refers to an error or unexpected situation that occurs when the program is running. Typically, exceptions cause the program to stop running and output an exception message.

How to use PHP's exception handling and fault tolerance mechanism? Introduction: In PHP programming, exception handling and fault tolerance mechanisms are very important. When errors or exceptions occur during code execution, exception handling can be used to capture and handle these errors to ensure program stability and reliability. This article will introduce how to use PHP's exception handling and fault tolerance mechanism. 1. Basic knowledge of exception handling: What is an exception? Exceptions are errors or abnormal conditions that occur during code execution, including syntax errors, runtime errors, logic errors, etc. When different

How to implement global exception handling in PHP backend function development? In PHP back-end development, exception handling is a very important part. It can help us catch errors in the program and handle them appropriately, thereby improving system stability and performance. This article will introduce how to implement global exception handling in PHP back-end function development and provide corresponding code examples. PHP provides an exception handling mechanism. We can catch exceptions and handle them accordingly through the try and catch keywords. Global exception handling refers to all

As the use of APIs becomes more and more widespread, we also need to consider data exception and error handling strategies during the development and use of APIs. This article will explore how PHP handles these issues when implementing APIs. 1. Handling data anomalies There may be many reasons for data anomalies, such as user input errors, network transmission errors, internal server errors, etc. When developing in PHP, we can use the following methods to handle data exceptions. Return the appropriate HTTP status code. The HTTP protocol defines many status codes that can help us

PHP time processing exception: error in returning time, specific code examples are needed. In web development, time processing is a very common requirement. As a commonly used server-side scripting language, PHP provides a wealth of time processing functions and methods. However, in practical applications, sometimes you encounter abnormal situations where the return time is wrong, which may be caused by errors in the code or improper use. In this article, we will introduce some common situations that may cause return time errors and provide some specific code examples to help readers better understand
