In the previous article, I brought you "Take you to understand the error types and error levels of PHP", which introduced the error types and error levels in PHP in detail. In this article we Let’s take a look at how to customize error handling and how to block errors in PHP. I hope it will be helpful to everyone!
In our daily development, it is inevitable to encounter errors. Sometimes we can specify a function as an error handling function. There is a custom error in PHP processing function.
set_error_handler()
##Function custom error handling function
set_error_handler(自定义函数名 [, int $error_types = E_ALL | E_STRICT ])
error_handler(int 错误的级别 , string 错误的信息 [, string 发生错误的文件名 [, int 发生错误的行号 ]])
"; echo "error msg:".$errstr."
"; echo "error file:".$errfile."
"; echo "error line:".$errline."
"; die('something error'); } set_error_handler("error_handler"); strpos(); ?>
"; echo "error msg:".$errstr."
"; echo "error file:".$errfile."
"; echo "error line:".$errline."
"; die('something error'); } set_error_handler("error_handler"); /* 触发异常 */ try { $a = 5/0;//程序会自动捕捉这个异常,并且由自定义函数来处理 echo $a; } catch(Exception $e) { echo $e->getMessage(); } ?>
##Error maskingIn the PHP development process, we can not only handle errors by customizing the error handling function through set_error_handler(), but we can also mask errors. In some cases, error masking is also essential. Next, I will list some methods for error shielding.
@##---Error control operator
Regarding the use of @, we need to note that the @ operator will only take effect when placed in front of an expression. For example, the @ operator can be used in front of variables, functions, constants, etc. , must not be placed before the definition of a function or class, nor can it be placed in front of a conditional structure statement.
Output result:
In the above example, it can be seen that @ Error control operator, which can mask expressions before expressions.
error_reporting()Function to shield errors##PHP There are many error levels in PHP. You can use the error_reporting() function to set what kind of errors PHP will report. The syntax format of the function is as follows:
error_reporting(设置错误级别)
##Mask errors through the
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. Let’s take a look at what to do. Do it.First we need to open the
(In the previous article "How to upload files in PHP? You will understand after reading it!" introduced the relevant knowledge on how to remove php and ini configuration files)
Examples are as follows:
This way you can block errors.
If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.
The above is the detailed content of Do you know how to customize error handling functions and error masking in PHP?. For more information, please follow other related articles on the PHP Chinese website!