Home > Article > Backend Development > Do you know how to customize error handling functions and error masking in PHP?
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.
<strong><span style="font-size: 20px;">set_error_handler() </span></strong>
##Function custom error handling function
set_error_handler(自定义函数名 [, int $error_types = E_ALL | E_STRICT ])The syntax format of this custom function name is as follows:
error_handler(int 错误的级别 , string 错误的信息 [, string 发生错误的文件名 [, int 发生错误的行号 ]])If there is an error handler defined before, the returned program name is the program name of the modified program; if it is a built-in error handler, the returned result is NULL. If an invalid callback function is specified, NULL will also be returned.
<?php function error_handler($errno, $errstr, $errfile, $errline ) { echo "error number:".$errno."<br/>"; echo "error msg:".$errstr."<br/>"; echo "error file:".$errfile."<br/>"; echo "error line:".$errline."<br/>"; die('something error'); } set_error_handler("error_handler"); strpos(); ?>Output results :
<?php function error_handler($errno, $errstr, $errfile, $errline ) { echo "error number:".$errno."<br/>"; echo "error msg:".$errstr."<br/>"; echo "error file:".$errfile."<br/>"; echo "error line:".$errline."<br/>"; die('something error'); } set_error_handler("error_handler"); /* 触发异常 */ try { $a = 5/0;//程序会自动捕捉这个异常,并且由自定义函数来处理 echo $a; } catch(Exception $e) { echo $e->getMessage(); } ?>Output result:
##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.
@<span style="font-size: 16px;"><strong></strong></span>##---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.
<?php $link = @mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db") or die('数据库连接失败!'); ?>
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(设置错误级别)
<?php error_reporting(0); $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db") or die('数据库连接失败!'); ?>
##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!