In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "custom errors in PHP how to deal with functions and error shielding", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PHP custom errors how to handle functions and error shielding" it!
In our daily development, it is inevitable to encounter errors. Sometimes we can specify a function as an error to handle the function. In PHP, there is a custom error handler.
The set_error_handler () function customizes the error handler
The set_error_handler () function provided in PHP is used to specify a function as an error handler. Its syntax format is as follows:
Set_error_handler (custom function name [, int $error_types = E_ALL | E_STRICT])
The syntax format of the custom function name is as follows:
Error_handler (level of int error, string error message [, file name of string error [, line number of int error]])
If there is a defined error handler before, the program name of the changed program is returned; if it is a built-in error handler, the result is NULL. If an invalid callback function is specified, NULL is also returned.
Next, let's take a look at an example, customize an error handler and use it to handle errors in the program. The example is as follows:
Output result:
What we need to pay attention to when using this function is that only in this way to handle errors, if the function has no errors, or the program does not run in the wrong function, the program will continue to execute the wrong function statements, so we need to use the die () function to terminate the function.
In our daily development, exceptions in the program cannot be thrown automatically, so we can also use set_error_handler () customization to treat exceptions as errors, so that we can use custom error handling to automatically catch exceptions.
Examples are as follows:
Output result:
Error shielding
In the PHP development process, we can not only handle errors through set_error_handler () custom error handlers, we can also mask errors, in some cases, error masking is also necessary. Then I'll give you some methods for error shielding.
@-error control operator
In PHP, put the error control operator @ in front of an expression, and any errors that may exist in that expression will be shielded.
With regard to the use of @, we need to note that the @ operator will not take effect until it is placed in front of the expression. For example, the @ operator can be used in front of variables, functions, constants, etc., and must not be placed before the definition of a function or class, nor before a conditional structure statement.
Examples are as follows:
Output result:
In the above example, you can see that the expression can be masked before the expression by @ error control operator.
Use the error_reporting () function to shield errors
There are many error levels in PHP. You can use the error_reporting () function to set what errors PHP will report. The syntax format of the function is as follows:
Error_reporting (set error level)
Error levels were described in more detail in the previous article, "taking you to understand the error types and error levels of PHP". Let's take a look at the use of functions through examples:
Output result:
Masking errors through display_errors parameter
This method is the most thorough method, the first two methods only work on a single line or a single file, but masking errors through the display_errors parameter works on all PHP files, so let's take a look at how it should be done.
First we need to open the php.ini configuration file, then find display_errors, and set its value to Off to turn off all PHP error reporting.
(in the previous article, "how does PHP upload files?" After reading it, you will understand! "knowledge of how to go to the php,ini configuration file is described in
Examples are as follows:
Thus, errors can be shielded.
Thank you for reading, the above is the content of "how to handle functions and error shielding in custom errors in PHP". After the study of this article, I believe you have a deeper understanding of how custom errors in PHP deal with functions and error shielding, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.