Do you know how to customize error handling functions and error masking in PHP?

WBOY
Release: 2023-04-10 18:12:02
Original
2592 people have browsed it

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!

Do you know how to customize error handling functions and error masking in PHP?

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

PHP provides the set_error_handler() function, which is used to specify a function as an error handling function. Its syntax format is as follows:

set_error_handler(自定义函数名 [, int $error_types = E_ALL | E_STRICT ])
Copy after login

The syntax format of this custom function name is as follows:

error_handler(int 错误的级别 , string 错误的信息 [, string 发生错误的文件名 [, int 发生错误的行号 ]])
Copy after login

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.


Next let’s take a look at an example, customize an error handling function, and use it to handle errors in the program. The example is as follows:


<?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(&#39;something error&#39;);
    }
    set_error_handler("error_handler");
    strpos();
?>
Copy after login

Output results :


Do you know how to customize error handling functions and error masking in PHP?

#What we need to pay attention to when using this function is that we only use this method for error handling. If the function has no errors, or the program does not When running in the wrong function, the program will continue to execute the function statement where the error occurred, so we need to use the die() function to terminate the function.


In our daily development, exceptions in the program cannot be thrown automatically. At this time, we can also use set_error_handler() to customize and handle the exception as an error, so that we can use Customize error handling to automatically catch exceptions.

The example is as follows:

<?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(&#39;something error&#39;);
    }
    set_error_handler("error_handler");
    /* 触发异常 */
    try {
        $a = 5/0;//程序会自动捕捉这个异常,并且由自定义函数来处理
        echo $a;
    } catch(Exception $e) {
        echo $e->getMessage();
    }
?>
Copy after login

Output result:


Do you know how to customize error handling functions and error masking in PHP?

##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

    In PHP, if the error control operator @ is placed in front of an expression, any errors that may exist in the expression will be blocked.

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.

The example is as follows:

<?php
    $link = @mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db") or die(&#39;数据库连接失败!&#39;);
?>
Copy after login

Output result:


In the above example, it can be seen that @ Error control operator, which can mask expressions before expressions. Do you know how to customize error handling functions and error masking in PHP?

  • Use

    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(设置错误级别)
    Copy after login
  • Regarding the error levels, please learn about them in the previous article "
PHP's error types and error levels are introduced in more detail. Next, let's look at the use of functions through examples:

<?php
    error_reporting(0);
    $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db") or die(&#39;数据库连接失败!&#39;);
?>
Copy after login

Output results:


Do you know how to customize error handling functions and error masking in PHP?
##Mask errors through the

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. Let’s take a look at what to do. Do it. First we need to open the

    php.ini
  • configuration file, then find display_errors, set its value to Off to turn off all PHP error reports.

(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:

Do you know how to customize error handling functions and error masking in PHP?

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template