In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the concept of errors and exceptions in PHP". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Understand the errors and exceptions of PHP together
In the process of learning PHP, we will come into contact with two concepts, one is error, the other is exception. What is it? Aren't they the same thing? If you have come into contact with pure object-oriented languages such as Java and C #, you may have no problem with exceptions. After all, all problems can be solved by try...catch. But for languages like PHP that have evolved 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 in place to deal with them, and how we should deal with them.
What is a mistake?
Errors are generally caused by PHP's own factors, wrong syntax, improper configuration of the environment, and so on. The error is directly related to the error_reporting parameter in the php.ini file. I believe everyone has matched this parameter. It is generally configured as E_ALL & ~ E_NOTICE. What does this mean? Let's first look at what types of errors there are in PHP:
Fatal Error: fatal error (script terminates)
E_ERROR / / fatal run error, error unrecoverable, fatal error in initialization during startup of paused script E_CORE_ERROR / / PHP E_COMPILE_ERROR / / fatal error at compile time, just like an E_ERRORE_USER_ERROR / / custom error message generated by the Zend script engine. Like using the PHP function trigger_error (error type is set to: E_USER_ERROR)
Parse Error: compile-time parsing error, syntax error (script terminates)
Syntax parsing errors during E_PARSE / / compilation
Warning Error: warning error (only prompt message is given, script does not stop running)
E_WARNING / / Runtime warning (non-fatal error). E_CORE_WARNING / / PHP initializes warnings (non-fatal errors) that occur during startup. E_COMPILE_WARNING / / compile warnings E_USER_WARNING / / user-generated warning messages
Notice Error: notification error (only notification information is given and the script does not stop running)
E_NOTICE / / Runtime notification. Indicates that the script encounters a situation that may appear as an error. notification information generated by the E_USER_NOTICE / / user.
E_ALL & ~ E_NOTICE in the configuration file means to display all errors except to notify error class errors. Of course, we can also manually change the notification of this error message in the code.
Error_reporting (E_ALL)
With this line of code, we show all the errors in the current file code. Errors of the Notice and Warning types do not interrupt the code; they are notifications and alarms, not fatal errors. Other types of errors 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
In the above code are the undefined variable hints for Warning divided by 0 error warning and echo $f;, both of which can continue to run down after an error is reported. Undefined methods are fatal errors at the Fatal level. So the last one will not be output.
So how to deal with mistakes? In principle, we should eliminate these errors, because basically they are not logical errors caused by the logic of our code, but real grammatical and environmental errors that should not occur in a production environment. At the same time, one of the most important differences between them and exceptions is that they cannot be caught through try...catch. In other words, there is no very good post-error handling 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); / / unable to capture
}
However, PHP still provides some error-handling functions for us to use.
Set_error_handler ()
Basically, you can only handle errors at the Warning and Notice levels.
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 you can see from the code, the fatal error of Fatal error is not caught.
Register_shutdown_function ()
In fact, it is not used to handle errors, the function of this function is the last function called before a fatal error occurs and the program stops. It can be used to log or close some important external handles, but in a production environment, we usually use log_error in php.ini for logging. 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/ to understand errors and exceptions in PHP (1) .php:16
/ / Stack trace:
/ / # 0 {main}
/ / thrown
/ / [file] = > / php/202002/source/ to understand errors and exceptions in PHP (1). Php
/ / [line] = > 16
/ /)
There are no parameter variables in the callback function of this function, so we need to error_get_last () to get all the error cases that occurred in this execution. It is also important to note that only errors generated at run time will be called into the callback of the registration function, and compile-time errors cannot be caught by this function, such as direct syntax errors:
Register_shutdown_function (function () {
Echo 'register_shutdown_function:', PHP_EOL
Print_r (error_get_last ())
});
Test (averse -); / / Parse error: syntax error, unexpected')'
This is the end of the introduction to the concept of errors and exceptions in PHP. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.