This article shares the usage of the PHP function register_shutdown_function for reference by friends in need.
The usage of php function register_shutdown_function is as follows: <?php function shutdown() { $last_error = error_get_last(); if ($last_error) { error_log(print_r($last_error, true), 3, ERROR_LOG); } } register_shutdown_function('shutdown'); ?> Copy after login Instructions: When the php script dies, you don't want to display a fatal error or a blank page to the user (when display_errors is set to off). There is a function in PHP called register_shutdown_function that allows us to set another function that can be called when execution is shut down. This function will be called when the script execution is completed or unexpectedly dies, causing PHP execution to be shut down. Therefore, we can use the method of setting a variable to false at the beginning of the script, and then setting it to true at the end of the script, so that The PHP shutdown callback function checks whether the script is complete. If our variable is still false, we know that the last line of the script was not executed, so it must have died somewhere in the program execution. The above example demonstrates how to give the user some appropriate feedback when a fatal error needs to be displayed. You can turn off the display of fatal errors (Annotation: you can set display_errors and error_reporting). |