PHP register_shutdown_function() Function

What is PHP register_shutdown_function() Function?

PHP register_shutdown_function() function is useful If you want a specific function to execute when a PHP script execution completes or exit() function is called. Check example 1.

Syntax:

register_shutdown_function(callback_function, argument1, argument2,…)

Parameters:

callback_function (Required): The callback_function is the function that you want to register and call when the script execution completes.

argument1, argument2,… (Optional): Arguments of the callback function. It can be one or more arguments.

Return Values:

The function doesn’t return any value.

Examples:

Example 1:

<?php
function my_function(){
   echo "Inside my_function.";
}
register_shutdown_function("my_function");
?>

Output:

Inside my_function.

Explanation:

Just before exiting from the script, the register_shutdown_function() calls the my_function() function. And this function prints “Inside my_function.”.

Example 2:

<?php
$connection = mysqli_connect("localhost", "username", "password", "db_name") or die("Couldn't connect to server");
function close_db_connection($connection){
    mysqli_close($connection);
}
register_shutdown_function("close_db_connection", $connection);
?>

Explanation:

Before closing the script. The register_shutdown_function() call the close_db_connection() function with the $connection argument. The close_db_connection() function closes the mysql database connection.

Example 3:

<?php
function log_err(){
    $err = error_get_last();
    if ($err) {
        $file = fopen("D:/xampp/htdocs/php/error_file.txt", "a");
        fwrite($file, date("Y-m-d H:i:s") . "\t" . "Error type: " . $err["type"] . "\t" . $err["message"] . "\n");
        fclose($file);
    }
}
register_shutdown_function("log_err");
echo abc();
?>

Output:

Fatal error: Uncaught Error: Call to undefined function abc() in D:\xampp\htdocs\phpExercise\register_shutdown_function Function.php:13 Stack trace: #0 {main} thrown in D:\xampp\htdocs\php\register_shutdown_function Function.php on line 13

Explanation:

The abc() function is not defined in the script but it is called in line 12. So, the fatal error occurs that you see in the output. Before closing the script, register_shutdown_function() function call the log_err() function which appends the error message in the error_file.txt file.

Practical Usages of register_shutdown_function() Function:

The register_shutdown_function() function is very useful to accomplish some important tasks like-

  • Cleaning up unfinished tasks: During an unexpected shutdown, there might have some ongoing tasks in your script. You may want to backup or cleanup the tasks by defining these in the callback function that will be called during the script termination.
  • Releasing resources: Before closing a PHP script, defining resource releasing tasks like database connection closing, unlinking file handles etc. are very effective for your application. You can define these tasks in the callback function which will be called by the register_shutdown_function() function during shutdown. Check example 2.
  • Logging errors: Occurring errors and handing those are parts of development process. You can use this function to log errors and exceptions to solve this later. Check example 3.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP register_shutdown_function() Function

register_shutdown_function() function is a built-in “function handling” type function. It is very useful to accomplish many tasks like cleaning up unfinished tasks, releasing resources, logging errors etc.

Reference:

https://www.php.net/manual/en/function.register-shutdown-function.php