<!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>Sets a user-defined error handler function</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.restore-exception-handler.html">restore_exception_handler</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.set-exception-handler.html">set_exception_handler</a></div> <div class="up"><a href="ref.errorfunc.html">Error Handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.set-error-handler" class="refentry"> <div class="refnamediv"> <h1 class="refname">set_error_handler</h1> <p class="verinfo">(PHP 4 >= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">set_error_handler</span> — <span class="dc-title">Sets a user-defined error handler function</span></p> </div> <div class="refsect1 description" id="refsect1-function.set-error-handler-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>set_error_handler</strong></span> ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$error_handler</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$error_types</code><span class="initializer"> = E_ALL | E_STRICT</span></span> ] )</div> <p class="para rdfs-comment"> Sets a user function (<em><code class="parameter">error_handler</code></em>) to handle errors in a script. </p> <p class="para"> This function can be used for defining your own way of handling errors during runtime, for example in applications in which you need to do cleanup of data/files when a critical error happens, or when you need to trigger an error under certain conditions (using <span class="function"><a href="function.trigger-error.html" class="function">trigger_error()</a></span>). </p> <p class="para"> It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by <em><code class="parameter">error_types</code></em> unless the callback function returns <strong><code>FALSE</code></strong>. <span class="function"><a href="function.error-reporting.html" class="function">error_reporting()</a></span> settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the <a href="language.operators.errorcontrol.html" class="link">@ error-control operator</a>. </p> <p class="para"> Also note that it is your responsibility to <span class="function"><a href="function.die.html" class="function">die()</a></span> if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error. </p> <p class="para"> The following error types cannot be handled with a user defined function: <strong><code>E_ERROR</code></strong>, <strong><code>E_PARSE</code></strong>, <strong><code>E_CORE_ERROR</code></strong>, <strong><code>E_CORE_WARNING</code></strong>, <strong><code>E_COMPILE_ERROR</code></strong>, <strong><code>E_COMPILE_WARNING</code></strong>, and most of <strong><code>E_STRICT</code></strong> raised in the file where <span class="function"><strong>set_error_handler()</strong></span> is called. </p> <p class="para"> If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time. </p> </div> <div class="refsect1 parameters" id="refsect1-function.set-error-handler-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">error_handler</code></em></span> <dd> <p class="para"> A callback with the following signature. <strong><code>NULL</code></strong> may be passed instead, to reset this handler to its default state. </p> <p class="para"> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><span class="replaceable">handler</span></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$errno</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$errstr</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$errfile</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$errline</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$errcontext</code></span> ]]] )</div> <dl> <dt> <span class="term"><em><code class="parameter">errno</code></em></span> <dd> <span class="simpara"> The first parameter, <em><code class="parameter">errno</code></em>, contains the level of the error raised, as an integer. </span> </dd> </dt> <dt> <span class="term"><em><code class="parameter">errstr</code></em></span> <dd> <span class="simpara"> The second parameter, <em><code class="parameter">errstr</code></em>, contains the error message, as a string. </span> </dd> </dt> <dt> <span class="term"><em><code class="parameter">errfile</code></em></span> <dd> <span class="simpara"> The third parameter is optional, <em><code class="parameter">errfile</code></em>, which contains the filename that the error was raised in, as a string. </span> </dd> </dt> <dt> <span class="term"><em><code class="parameter">errline</code></em></span> <dd> <span class="simpara"> The fourth parameter is optional, <em><code class="parameter">errline</code></em>, which contains the line number the error was raised at, as an integer. </span> </dd> </dt> <dt> <span class="term"><em><code class="parameter">errcontext</code></em></span> <dd> <span class="simpara"> The fifth parameter is optional, <em><code class="parameter">errcontext</code></em>, which is an array that points to the active symbol table at the point the error occurred. In other words, <em><code class="parameter">errcontext</code></em> will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context. </span> </dd> </dt> </dl> </p> <p class="para"> If the function returns <strong><code>FALSE</code></strong> then the normal error handler continues. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">error_types</code></em></span> <dd> <p class="para"> Can be used to mask the triggering of the <em><code class="parameter">error_handler</code></em> function just like the <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> ini setting controls which errors are shown. Without this mask set the <em><code class="parameter">error_handler</code></em> will be called for every error regardless to the setting of the <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> setting. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.set-error-handler-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a string containing the previously defined error handler (if any). If the built-in error handler is used <strong><code>NULL</code></strong> is returned. <strong><code>NULL</code></strong> is also returned in case of an error such as an invalid callback. If the previous error handler was a class method, this function will return an indexed array with the class and the method name. </p> </div> <div class="refsect1 changelog" id="refsect1-function.set-error-handler-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>5.5.0</td> <td> <em><code class="parameter">error_handler</code></em> now accepts <strong><code>NULL</code></strong>. </td> </tr> <tr> <td>5.2.0</td> <td> The error handler must return <strong><code>FALSE</code></strong> to populate <var class="varname"><var class="varname"><a href="reserved.variables.phperrormsg.html" class="classname">$php_errormsg</a></var></var>. </td> </tr> <tr> <td>5.0.0</td> <td> The <em><code class="parameter">error_types</code></em> parameter was introduced. </td> </tr> <tr> <td>4.3.0</td> <td> Instead of a function name, an array containing an object reference and a method name can also be supplied as the <em><code class="parameter">error_handler</code></em>. </td> </tr> <tr> <td>4.0.2</td> <td> Three optional parameters for the <em><code class="parameter">error_handler</code></em> user function was introduced. These are the filename, the line number, and the context. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.set-error-handler-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-456"> <p><strong>Example #1 Error handling with <span class="function"><strong>set_error_handler()</strong></span> and <span class="function"><a href="function.trigger-error.html" class="function">trigger_error()</a></span></strong></p> <div class="example-contents"><p> The example below shows the handling of internal exceptions by triggering errors and handling them with a user defined function: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// error handler function<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">myErrorHandler</span><span style="color: #007700">(</span><span style="color: #0000BB">$errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$errstr</span><span style="color: #007700">, </span><span style="color: #0000BB">$errfile</span><span style="color: #007700">, </span><span style="color: #0000BB">$errline</span><span style="color: #007700">)<br />{<br /> if (!(</span><span style="color: #0000BB">error_reporting</span><span style="color: #007700">() & </span><span style="color: #0000BB">$errno</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// This error code is not included in error_reporting<br /> </span><span style="color: #007700">return;<br /> }<br /><br /> switch (</span><span style="color: #0000BB">$errno</span><span style="color: #007700">) {<br /> case </span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"<b>My ERROR</b> [</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">] </span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">" Fatal error on line </span><span style="color: #0000BB">$errline</span><span style="color: #DD0000"> in file </span><span style="color: #0000BB">$errfile</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">", PHP " </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_VERSION </span><span style="color: #007700">. </span><span style="color: #DD0000">" (" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_OS </span><span style="color: #007700">. </span><span style="color: #DD0000">")<br />\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"Aborting...<br />\n"</span><span style="color: #007700">;<br /> exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /> break;<br /><br /> case </span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"<b>My WARNING</b> [</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">] </span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br /> break;<br /><br /> case </span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"<b>My NOTICE</b> [</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">] </span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br /> break;<br /><br /> default:<br /> echo </span><span style="color: #DD0000">"Unknown error type: [</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">] </span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br /> break;<br /> }<br /><br /> </span><span style="color: #FF8000">/* Don't execute PHP internal error handler */<br /> </span><span style="color: #007700">return </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// function to test the error handling<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$vect</span><span style="color: #007700">, </span><span style="color: #0000BB">$scale</span><span style="color: #007700">)<br />{<br /> if (!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$scale</span><span style="color: #007700">) || </span><span style="color: #0000BB">$scale </span><span style="color: #007700"><= </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"log(x) for x <= 0 is undefined, you used: scale = </span><span style="color: #0000BB">$scale</span><span style="color: #DD0000">"</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br /> }<br /><br /> if (!</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$vect</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Incorrect input vector, array of values expected"</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /> }<br /><br /> </span><span style="color: #0000BB">$temp </span><span style="color: #007700">= array();<br /> foreach(</span><span style="color: #0000BB">$vect </span><span style="color: #007700">as </span><span style="color: #0000BB">$pos </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> if (!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Value at position </span><span style="color: #0000BB">$pos</span><span style="color: #DD0000"> is not a number, using 0 (zero)"</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">$temp</span><span style="color: #007700">[</span><span style="color: #0000BB">$pos</span><span style="color: #007700">] = </span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">$scale</span><span style="color: #007700">) * </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br /> }<br /><br /> return </span><span style="color: #0000BB">$temp</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// set to the user defined error handler<br /></span><span style="color: #0000BB">$old_error_handler </span><span style="color: #007700">= </span><span style="color: #0000BB">set_error_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">"myErrorHandler"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// trigger some errors, first define a mixed array with a non-numeric item<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"vector a\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #0000BB">5.5</span><span style="color: #007700">, </span><span style="color: #0000BB">43.3</span><span style="color: #007700">, </span><span style="color: #0000BB">21.11</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// now generate second array<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"----\nvector b - a notice (b = log(PI) * a)\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* Value at position $pos is not a number, using 0 (zero) */<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">M_PI</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// this is trouble, we pass a string instead of an array<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"----\nvector c - a warning\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* Incorrect input vector, array of values expected */<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #DD0000">"not array"</span><span style="color: #007700">, </span><span style="color: #0000BB">2.3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">); </span><span style="color: #FF8000">// NULL<br /><br />// this is a critical error, log of zero or negative number is undefined<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"----\nvector d - fatal error\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* log(x) for x <= 0 is undefined, you used: scale = $scale" */<br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= </span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, -</span><span style="color: #0000BB">2.5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">); </span><span style="color: #FF8000">// Never reached<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> vector a Array ( [0] => 2 [1] => 3 [2] => foo [3] => 5.5 [4] => 43.3 [5] => 21.11 ) ---- vector b - a notice (b = log(PI) * a) <b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br /> Array ( [0] => 2.2894597716988 [1] => 3.4341896575482 [2] => 0 [3] => 6.2960143721717 [4] => 49.566804057279 [5] => 24.165247890281 ) ---- vector c - a warning <b>My WARNING</b> [512] Incorrect input vector, array of values expected<br /> NULL ---- vector d - fatal error <b>My ERROR</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br /> Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br /> Aborting...<br /> </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.set-error-handler-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="class.errorexception.html" class="classname">ErrorException</a></li> <li class="member"> <span class="function"><a href="function.error-reporting.html" class="function" rel="rdfs-seeAlso">error_reporting()</a> - Sets which PHP errors are reported</span></li> <li class="member"> <span class="function"><a href="function.restore-error-handler.html" class="function" rel="rdfs-seeAlso">restore_error_handler()</a> - Restores the previous error handler function</span></li> <li class="member"> <span class="function"><a href="function.trigger-error.html" class="function" rel="rdfs-seeAlso">trigger_error()</a> - Generates a user-level error/warning/notice message</span></li> <li class="member"><a href="errorfunc.constants.html" class="link">error level constants</a></li> <li class="member">information about the <a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> type</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.restore-exception-handler.html">restore_exception_handler</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.set-exception-handler.html">set_exception_handler</a></div> <div class="up"><a href="ref.errorfunc.html">Error Handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>