<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Register a function for execution on shutdown</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.get-defined-functions.html">get_defined_functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.register-tick-function.html">register_tick_function</a></div> <div class="up"><a href="ref.funchand.html">Function handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.register-shutdown-function" class="refentry"> <div class="refnamediv"> <h1 class="refname">register_shutdown_function</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">register_shutdown_function</span> — <span class="dc-title">Register a function for execution on shutdown</span></p> </div> <div class="refsect1 description" id="refsect1-function.register-shutdown-function-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><strong>register_shutdown_function</strong></span> ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$parameter</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span> ]] )</div> <p class="para rdfs-comment"> Registers a <em><code class="parameter">callback</code></em> to be executed after script execution finishes or <span class="function"><a href="function.exit.html" class="function">exit()</a></span> is called. </p> <p class="para"> Multiple calls to <span class="function"><strong>register_shutdown_function()</strong></span> can be made, and each will be called in the same order as they were registered. If you call <span class="function"><a href="function.exit.html" class="function">exit()</a></span> within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called. </p> </div> <div class="refsect1 parameters" id="refsect1-function.register-shutdown-function-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">callback</code></em></span> <dd> <p class="para"> The shutdown callback to register. </p> <p class="para"> The shutdown callbacks are executed as the part of the request, so it's possible to send output from them and access output buffers. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">parameter</code></em></span> <dd> <p class="para"> It is possible to pass parameters to the shutdown function by passing additional parameters. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">...</code></em></span> <dd> <p class="para"> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.register-shutdown-function-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> No value is returned. </p> </div> <div class="refsect1 errors" id="refsect1-function.register-shutdown-function-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> If the passed callback is not callable a <strong><code>E_WARNING</code></strong> level error will be generated. </p> </div> <div class="refsect1 changelog" id="refsect1-function.register-shutdown-function-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.1.0</td> <td> The shutdown functions are now called as a part of the request. In earlier versions under Apache, the registered shutdown functions were called after the request has been completed (including sending any output buffers), so it was not possible to send output to the browser using <span class="function"><a href="function.echo.html" class="function">echo</a></span> or <span class="function"><a href="function.print.html" class="function">print</a></span>, or retrieve the contents of any output buffers using <span class="function"><a href="function.ob-get-contents.html" class="function">ob_get_contents()</a></span>. Headers were also always already sent. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.register-shutdown-function-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5118"> <p><strong>Example #1 <span class="function"><strong>register_shutdown_function()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">shutdown</span><span style="color: #007700">()<br />{<br /> </span><span style="color: #FF8000">// This is our shutdown function, in <br /> // here we can do any last operations<br /> // before the script is complete.<br /><br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">'Script executed with success'</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">register_shutdown_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'shutdown'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.register-shutdown-function-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Shutdown functions will not be executed if the process is killed with a SIGTERM or SIGKILL signal. While you cannot intercept a SIGKILL, you can use <span class="function"><a href="function.pcntl-signal.html" class="function">pcntl_signal()</a></span> to install a handler for a SIGTERM which uses <span class="function"><a href="function.exit.html" class="function">exit()</a></span> to end cleanly. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.register-shutdown-function-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="ini.core.html#ini.auto-append-file" class="link">auto_append_file</a></li> <li class="member"> <span class="function"><a href="function.exit.html" class="function" rel="rdfs-seeAlso">exit()</a> - Output a message and terminate the current script</span></li> <li class="member">The section on <a href="features.connection-handling.html" class="link">connection handling</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.get-defined-functions.html">get_defined_functions</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.register-tick-function.html">register_tick_function</a></div> <div class="up"><a href="ref.funchand.html">Function handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>