Some simple error handling functions (1)

Time:2021-10-18

In the previous article, we learned the difference between exceptions and errors in PHP, and also briefly introduced some error handling functions in PHP. This time, we will open two more articles to introduce in detail some functions related to error handling in PHP. To learn about errors and exceptions, move to:

error_reporting()

This function must be touched by everyone, that is, to define the error handling mechanism of PHP at runtime. For example, when debugging, we often need to set an E_ All to display all error messages.

error_reporting(E_ALL);

This function can only be defined as constants for error handling provided internally in PHP, including:

  • Fatal error: fatal error (script terminated)

    • E_ Error / / fatal running error. The error cannot be recovered. Pause script execution
    • E_ CORE_ Error / / fatal error during initialization during PHP startup
    • E_ COMPILE_ Error / / fatal error during compilation, like an e generated by Zend script engine_ ERROR
    • E_ USER_ Error / / custom error message. Like using the PHP function trigger_ Error (the error type is set to e_user_error)
  • Parse error: parsing error and syntax error during compilation (script termination)

    • E_ Parse / / syntax parsing error during compilation
  • Warning error: warning error (only prompt information is given, and the script does not terminate)

    • E_ Warning / / runtime warning (non fatal error).
    • E_ CORE_ Warning / / a warning (non fatal error) that occurs during PHP initialization startup.
    • E_ COMPILE_ Warning / / compilation warning
    • E_ USER_ Warning / / a warning message generated by the user
  • Notice error: notification error (only notification information is given, and the script does not terminate)

    • E_ Note / / runtime notification. Indicates that the script encountered a situation that might appear as an error
    • E_ USER_ Note / / the notification information generated by the user.

Of course, this function can also be globally configured through the php.ini file. The specific configuration method will not be repeated here. There will also be detailed instructions in the comments including the php.ini file.

error_get_last()

error_ get_ The last () function returns our last error message. It returns an array containing the “type”, “message”, “file” and “line” information of the error message, which is convenient for us to view the specific location and content of the error.

echo $a; // Notice: Undefined variable: a
print_r(error_get_last());
// Array
// (
//     [type] => 8
//     [message] => Undefined variable: a
//[file] = > / users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / some simple error handling functions (I). PHP
//     [line] => 5
// )
echo $b;
print_ r(error_get_last()); //  The problem of $a will not be printed
// Array
// (
//     [type] => 8
//     [message] => Undefined variable: b
//[file] = > / users / ZhangYue / MyDoc / blog post / dev blog / PHP / 202004 / source / some simple error handling functions (I). PHP
//     [line] => 17
// )

echo $a;
echo $b;
print_ r(error_get_last()); //  Also, only $B questions will be printed

Note that it only returns the last error message. For example, echo $a in the last paragraph of the above example; And echo $B; Will produce errors, but the final print out is echo $B; Error message generated.

error_clear_last

As can be seen from the name, this function is used to clear the last error message. That is to say, if the function is called after the wrong code, error_ get_ Last () will not print anything.

echo $a; // Notice: Undefined variable: a
error_clear_last();
print_ r(error_get_last()); //  No output

error_log

Finally, let’s look at a function of error logging. It can not only record the log to the log file, but also send mail directly.

error_log("Test Error One!");
//Error defined in php.ini_ Log file
// [22-Apr-2020 09:04:34 Asia/Shanghai] Test Error One!

error_log("Test Error One!", 1, "[email protected]");

echo $a;
error_log(base64_encode(json_encode(error_get_last())), 1, "[email protected]");

In the first paragraph, we only have one parameter, so the error information will be directly recorded in the error log defined in the php.ini file. The latter two paragraphs send the content to a mailbox.

The declaration form of this function is:

error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] ) : bool
  • $message, error message content, text form
  • $message_ Type, where the error is sent. The default is 0 system log file. 1 is sent to the mail address defined by $destination, 3 is sent to the mail address defined by $destination, and $message will not be treated as a new line. 4 is sent to the log handler of SAPI
  • $destination, usually email address
  • $extra_ Headers, extra headers, in $message_ Useful when type is 1

One thing to note about this function is that the $message content cannot have null or other symbols that may truncate characters. So in our test code, send error_ get_ When we last () the content, we not only convert it into JSON, but also add a layer of Base64 coding to ensure the normal sending of the content.

summary

This article mainly introduces these processing functions for error situations. The surprise is error_ Log() function, which does not need too much configuration, can directly send mail through PHP’s own mail. Maybe we can try to monitor and track some errors in our production environment!!

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202004/source/%E4%B8%80%E4%BA%9B%E7%AE%80%E5%8D%95%E7%9A%84%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%E5%87%BD%E6%95%B0%EF%BC%88%E4%B8%80%EF%BC%89.php

Reference documents:

https://www.php.net/manual/zh/function.error-reporting.php
https://www.php.net/manual/zh/function.error-get-last.php
https://www.php.net/manual/zh/function.error-clear-last.php
https://www.php.net/manual/zh/function.error-log.php

===========

Each media platform can search [hard core project manager]