⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.set-error-handler.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
       </dl>      </p>      <p class="para">       If the function returns <b><tt>FALSE</tt></b> then the normal error handler continues.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">error_types</tt></i></span>     <dd>      <p class="para">       Can be used to mask the triggering of the       <i><tt class="parameter">error_handler</tt></i> 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       <i><tt class="parameter">error_handler</tt></i> 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">  <h3 class="title">Return Values</h3>  <p class="para">   Returns a string containing the previously defined   error handler (if any), or <b><tt>NULL</tt></b> on error. If the previous 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">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">5.2.0</td>       <td colspan="1" rowspan="1" align="left">        The error handler must return <b><tt>FALSE</tt></b> to populate        <var class="varname"><a href="reserved.variables.phperrormsg.html" class="classname">$php_errormsg</a></var>.       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">5.0.0</td>       <td colspan="1" rowspan="1" align="left">        The <i><tt class="parameter">error_types</tt></i> parameter was introduced.       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.3.0</td>       <td colspan="1" rowspan="1" align="left">        Instead of a function name, an array containing an object reference         and a method name can also be supplied as the        <i><tt class="parameter">error_handler</tt></i>.       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.0.2</td>       <td colspan="1" rowspan="1" align="left">        Three optional parameters for the <i><tt class="parameter">error_handler</tt></i>        user function was introduced. These are the filename, the line number,         and the context.       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 Error handling with <b>set_error_handler()</b> and <a href="function.trigger-error.html" class="function">trigger_error()</a></b></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">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;error&nbsp;handler&nbsp;function<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">myErrorHandler</span><span style="color: #007700">(</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errfile</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errline</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(</span><span style="color: #0000BB">$errno</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;My&nbsp;ERROR&lt;/b&gt;&nbsp;[$errno]&nbsp;$errstr&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;Fatal&nbsp;error&nbsp;on&nbsp;line&nbsp;$errline&nbsp;in&nbsp;file&nbsp;$errfile"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">",&nbsp;PHP&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_VERSION&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&nbsp;("&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_OS&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">")&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Aborting...&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;My&nbsp;WARNING&lt;/b&gt;&nbsp;[$errno]&nbsp;$errstr&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;My&nbsp;NOTICE&lt;/b&gt;&nbsp;[$errno]&nbsp;$errstr&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Unknown&nbsp;error&nbsp;type:&nbsp;[$errno]&nbsp;$errstr&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Don't&nbsp;execute&nbsp;PHP&nbsp;internal&nbsp;error&nbsp;handler&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;function&nbsp;to&nbsp;test&nbsp;the&nbsp;error&nbsp;handling<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$vect</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$scale</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$scale</span><span style="color: #007700">)&nbsp;||&nbsp;</span><span style="color: #0000BB">$scale&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"log(x)&nbsp;for&nbsp;x&nbsp;&lt;=&nbsp;0&nbsp;is&nbsp;undefined,&nbsp;you&nbsp;used:&nbsp;scale&nbsp;=&nbsp;$scale"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$vect</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Incorrect&nbsp;input&nbsp;vector,&nbsp;array&nbsp;of&nbsp;values&nbsp;expected"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$temp&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach(</span><span style="color: #0000BB">$vect&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Value&nbsp;at&nbsp;position&nbsp;$pos&nbsp;is&nbsp;not&nbsp;a&nbsp;number,&nbsp;using&nbsp;0&nbsp;(zero)"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$temp</span><span style="color: #007700">[</span><span style="color: #0000BB">$pos</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">$scale</span><span style="color: #007700">)&nbsp;*&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$temp</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;set&nbsp;to&nbsp;the&nbsp;user&nbsp;defined&nbsp;error&nbsp;handler<br /></span><span style="color: #0000BB">$old_error_handler&nbsp;</span><span style="color: #007700">=&nbsp;</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">//&nbsp;trigger&nbsp;some&nbsp;errors,&nbsp;first&nbsp;define&nbsp;a&nbsp;mixed&nbsp;array&nbsp;with&nbsp;a&nbsp;non-numeric&nbsp;item<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"vector&nbsp;a\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">43.3</span><span style="color: #007700">,&nbsp;</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">//&nbsp;now&nbsp;generate&nbsp;second&nbsp;array<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"----\nvector&nbsp;b&nbsp;-&nbsp;a&nbsp;notice&nbsp;(b&nbsp;=&nbsp;log(PI)&nbsp;*&nbsp;a)\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;Value&nbsp;at&nbsp;position&nbsp;$pos&nbsp;is&nbsp;not&nbsp;a&nbsp;number,&nbsp;using&nbsp;0&nbsp;(zero)&nbsp;*/<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</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">//&nbsp;this&nbsp;is&nbsp;trouble,&nbsp;we&nbsp;pass&nbsp;a&nbsp;string&nbsp;instead&nbsp;of&nbsp;an&nbsp;array<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"----\nvector&nbsp;c&nbsp;-&nbsp;a&nbsp;warning\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;Incorrect&nbsp;input&nbsp;vector,&nbsp;array&nbsp;of&nbsp;values&nbsp;expected&nbsp;*/<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #DD0000">"not&nbsp;array"</span><span style="color: #007700">,&nbsp;</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">);&nbsp;</span><span style="color: #FF8000">//&nbsp;NULL<br /><br />//&nbsp;this&nbsp;is&nbsp;a&nbsp;critical&nbsp;error,&nbsp;log&nbsp;of&nbsp;zero&nbsp;or&nbsp;negative&nbsp;number&nbsp;is&nbsp;undefined<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"----\nvector&nbsp;d&nbsp;-&nbsp;fatal&nbsp;error\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;log(x)&nbsp;for&nbsp;x&nbsp;&lt;=&nbsp;0&nbsp;is&nbsp;undefined,&nbsp;you&nbsp;used:&nbsp;scale&nbsp;=&nbsp;$scale"&nbsp;*/<br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;-</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">);&nbsp;</span><span style="color: #FF8000">//&nbsp;Never&nbsp;reached<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>    <div class="example-contents"><p>The above example will output something similar to:</p></div>    <div class="example-contents"><pre><div class="cdata"><pre>vector aArray(    [0] =&gt; 2    [1] =&gt; 3    [2] =&gt; foo    [3] =&gt; 5.5    [4] =&gt; 43.3    [5] =&gt; 21.11)----vector b - a notice (b = log(PI) * a)&lt;b&gt;My NOTICE&lt;/b&gt; [1024] Value at position 2 is not a number, using 0 (zero)&lt;br /&gt;Array(    [0] =&gt; 2.2894597716988    [1] =&gt; 3.4341896575482    [2] =&gt; 0    [3] =&gt; 6.2960143721717    [4] =&gt; 49.566804057279    [5] =&gt; 24.165247890281)----vector c - a warning&lt;b&gt;My WARNING&lt;/b&gt; [512] Incorrect input vector, array of values expected&lt;br /&gt;NULL----vector d - fatal error&lt;b&gt;My ERROR&lt;/b&gt; [256] log(x) for x &lt;= 0 is undefined, you used: scale = -2.5&lt;br /&gt;  Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)&lt;br /&gt;Aborting...&lt;br /&gt;</pre></div>    </pre></div>   </div>  </p> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.error-reporting.html" class="function" rel="rdfs-seeAlso">error_reporting()</a></li>    <li class="member"><a href="function.restore-error-handler.html" class="function" rel="rdfs-seeAlso">restore_error_handler()</a></li>    <li class="member"><a href="function.trigger-error.html" class="function" rel="rdfs-seeAlso">trigger_error()</a></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 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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -