During the PHP learning process, we will come into contact with two concepts, one is error and the other is exception. What's the thing? Are they the same thing? If you have been exposed to pure object-oriented languages such as Java and C#, you may have no problem with exceptions. After all, all problems can be solved by trying...catch. But in terms of languages like PHP that develops from process-oriented to object-oriented, errors and exceptions are two completely different things.
We will use a series of articles to thoroughly understand what errors and exceptions are in PHP, what mechanisms are there for dealing with these errors and exceptions, and how we should treat them.
What is a mistake?
Errors are generally caused by factors of PHP themselves. Incorrect syntax and improper environment configuration will cause errors. The error has a direct relationship with the error_reporting parameter in the file. I believe everyone has this parameter. It is usually configured as E_ALL & ~E_NOTICE. What does this mean? Let's first look at what types of errors are in PHP:
Fatal Error: Fatal error (the script terminates running)
- E_ERROR // Fatal running error, the error cannot be restored, the execution of the script is paused
- E_CORE_ERROR // Fatal error during initialization process during PHP startup
- E_COMPILE_ERROR // Fatal error when compiling, just like an E_ERROR generated by the Zend script engine
- E_USER_ERROR // Custom error message. For example, use the PHP function trigger_error (Error type is set to: E_USER_ERROR)
Parse Error: Compile-time parsing error, syntax error (script terminates running)
E_PARSE // Syntax parsing errors during compilation
Warning Error: Warning error (only give prompt information, the script does not terminate running)
- E_WARNING // Runtime warning (non-fatal error).
- E_CORE_WARNING // Warning (non-fatal error) that occurs during PHP initialization startup.
- E_COMPILE_WARNING // Compile warning
- E_USER_WARNING // User generated warning message
Notice Error: Notification error (only notification information is given, the script does not terminate operation)
- E_NOTICE // Runtime notification. Indicates that the script encounters a situation that may appear as an error.
- E_USER_NOTICE // User generated notification information.
E_ALL & ~E_NOTICE in the configuration file means to display all errors except notification error-like errors. Of course, we can also manually change the notification of this error message in the code.
error_reporting(E_ALL);
Through this line of code, we will display all the errors in the current file code. Notice and Warning type errors do not interrupt code running. They are notifications and alarms, not fatal errors. Other types of errors will interrupt the execution of the code.
$a = 100 / 0; // Warning: Division by zero echo $f; // Notice: Undefined variable: f test(); // Fatal error: Uncaught Error: Call to undefined function test() echo 1;
Among the above codes, Warning's error warning divided by 0 and echo $f; are respectively undefined variable prompts. Both lines of code can continue to run downward after an error is reported. The undefined method is a fatal error at the Fatal level. So the last 1 won't output either.
So how to deal with errors? In principle, we should eliminate these errors, because they are basically logical errors caused by the logic of our code not being clarified. They are real syntax and environment errors, which should not occur in the production environment. At the same time, the most important difference between them and exceptions is that they cannot be captured through try...catch. That is to say, there is no very good error post-processing mechanism for this kind of error.
try { $a = 100 / 0; // Warning: Division by zero echo $f; // Notice: Undefined variable: f } catch (Excepiton $e) { print_r($e); // Cannot capture}
However, PHP still provides some functions for us to handle errors.
- set_error_handler()
Basically, it can only handle Warning and Notice levels errors.
set_error_handler(function( $errno , $errstr ){ echo 'set_error_handler:', $errno, $errstr, PHP_EOL; }); $a = 100 / 0; // Warning: Division by zero echo $f; // Notice: Undefined variable: f test(); // Fatal error: Uncaught Error: Call to undefined function test() // set_error_handler:2Division by zero // set_error_handler:8Undefined variable: f
As can be seen from the code, the fatal error like Fatal error is not caught.
- register_shutdown_function()
In fact, it is not used to deal with errors. The function is a function that will be called last before a fatal error occurs and the program stops. It can be used to record logs or close some important external handles, but in production environments, we generally use the log_error in the log to record logs. So this function is not used much.
register_shutdown_function(function(){ echo 'register_shutdown_function:', PHP_EOL; print_r(error_get_last()); }); test(); // register_shutdown_function: // Array // ( // [type] => 1 // [message] => Uncaught Error: Call to undefined function test() in /php/202002/source/Together to understand PHP errors and exceptions (I).php:16// Stack trace: // #0 {main} // thrown // [file] => /php/202002/source/Together to understand PHP errors and exceptions (I).php// [line] => 16 // )
The callback function of this function does not have any parameter variables, so we need to use error_get_last() to get all the errors that occurred in this execution. It should be noted that only errors generated at runtime will be called into the callback of this registered function. Compilation errors cannot be captured through this function, such as direct syntax errors:
register_shutdown_function(function(){ echo 'register_shutdown_function:', PHP_EOL; print_r(error_get_last()); }); test(a+-); // Parse error: syntax error, unexpected ')'
Summarize
To sum up, as mentioned earlier in the article, errors should be not brought to the production environment as much as possible, and they do not have a good handling mechanism. In other words, errors are things we should try to avoid, because in most cases it has nothing to do with our logical code. Moreover, serious errors will directly cause the program to be aborted, and the program cannot be maintained through the catch mechanism like an exception.
The above is the detailed content of the concepts of errors and exceptions in PHP. For more information about PHP errors and exceptions, please pay attention to my other related articles!