PHP register_shutdown_function() Function
The PHP register_shutdown_function() function is used to register a function for execution on shutdown. The registered shutdown function is executed after script execution finishes or exit() is called.
Multiple calls to register_shutdown_function() can be made, and each will be called in the same order as they were registered. If exit() is called within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called.
Syntax
register_shutdown_function(callback, args)
Parameters
callback |
Required. Specify the shutdown callback to register. The shutdown callbacks are executed as the part of the request, so it's possible to send output from them and access output buffers. |
args |
Optional. Specify parameters to the shutdown function by passing additional parameters. |
Return Value
No value is returned.
Exceptions
Generates a E_WARNING level error if the passed callback is not callable.
Example: register_shutdown_function() example
The example below shows the usage of register_shutdown_function() function.
<?php //defining a shutdown function function shutdown() { echo 'Script is executed successfully!'.PHP_EOL; } //registering the shutdown function register_shutdown_function('shutdown'); //displaying some message echo "Hello World!\n"; ?>
The output of the above code will be similar to:
Hello World! Script is executed successfully!
Example: register_shutdown_function() example
In the example below the shutdown function is used to display message based on connection status.
<?php //defining a shutdown function function shutdown() { if(connection_status() == CONNECTION_TIMEOUT) { print 'Script timeout!'.PHP_EOL; } else { print 'Script is executed successfully!'.PHP_EOL; } } //registering the shutdown function register_shutdown_function('shutdown'); //displaying some message echo "Hello World!\n"; ?>
The output of the above code will be similar to:
Hello World! Script is executed successfully!
Example: using args parameter
Consider one more example, where this function is used to pass an additional parameter to the shutdown function.
<?php //defining a shutdown function function shutdown($user) { if(connection_status() == CONNECTION_TIMEOUT) { print "Script timeout!".PHP_EOL; } else { print "Goodbye $user!".PHP_EOL; } } $user = "John"; //registering the shutdown function register_shutdown_function('shutdown', $user); //displaying some message echo "Hello $user!\n"; ?>
The output of the above code will be similar to:
Hello John! Goodbye John!
❮ PHP Function Handling Reference