<p >set_error_handler<br /> (PHP 4 &gt;= 4.0.1, PHP 5)</p> <p >set_error_handler -- Sets a user-defined error handler function</p> <p ></p> <p >例子 1. Error handling with set_error_handler() and trigger_error()</p> <p >The example below shows the handling of internal exceptions by triggering errors and handling them with a user defined function:</p> <p >&lt;?php<br /> // set the error reporting level for this script<br /> error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);</p> <p >// error handler function<br /> function myErrorHandler($errno, $errstr, $errfile, $errline)<br /> {<br /> switch ($errno) {<br /> case E_USER_ERROR:<br /> echo &quot;&lt;b&gt;My ERROR&lt;/b&gt; [$errno] $errstr&lt;br /&gt;n&quot;;<br /> echo &quot; Fatal error in line $errline of file $errfile&quot;;<br /> echo &quot;, PHP &quot; . PHP_VERSION . &quot; (&quot; . PHP_OS . &quot;)&lt;br /&gt;n&quot;;<br /> echo &quot;Aborting...&lt;br /&gt;n&quot;;<br /> exit(1);<br /> break;<br /> case E_USER_WARNING:<br /> echo &quot;&lt;b&gt;My WARNING&lt;/b&gt; [$errno] $errstr&lt;br /&gt;n&quot;;<br /> break;<br /> case E_USER_NOTICE:<br /> echo &quot;&lt;b&gt;My NOTICE&lt;/b&gt; [$errno] $errstr&lt;br /&gt;n&quot;;<br /> break;<br /> default:<br /> echo &quot;Unkown error type: [$errno] $errstr&lt;br /&gt;n&quot;;<br /> break;<br /> }<br /> }</p> <p >// function to test the error handling<br /> function scale_by_log($vect, $scale)<br /> {<br /> if (!is_numeric($scale) || $scale &lt;= 0) {<br /> trigger_error(&quot;log(x) for x &lt;= 0 is undefined, you used: scale = $scale&quot;, E_USER_ERROR);<br /> }</p> <p > if (!is_array($vect)) {<br /> trigger_error(&quot;Incorrect input vector, array of values expected&quot;, E_USER_WARNING);<br /> return null;<br /> }</p> <p > for ($i=0; $i&lt;count($vect); $i++) {<br /> if (!is_numeric($vect[$i]))<br /> trigger_error(&quot;Value at position $i is not a number, using 0 (zero)&quot;, E_USER_NOTICE);<br /> $temp[$i] = log($scale) * $vect[$i];<br /> }<br /> return $temp;<br /> }</p> <p >// set to the user defined error handler<br /> $old_error_handler = set_error_handler(&quot;myErrorHandler&quot;);</p> <p >// trigger some errors, first define a mixed array with a non-numeric item<br /> echo &quot;vector an&quot;;<br /> $a = array(2,3, &quot;foo&quot;, 5.5, 43.3, 21.11);<br /> print_r($a);</p> <p >// now generate second array, generating a warning<br /> echo &quot;----nvector b - a warning (b = log(PI) * a)n&quot;;<br /> $b = scale_by_log($a, M_PI);<br /> print_r($b);</p> <p >// this is trouble, we pass a string instead of an array<br /> echo &quot;----nvector c - an errorn&quot;;<br /> $c = scale_by_log(&quot;not array&quot;, 2.3);<br /> var_dump($c);</p> <p >// this is a critical error, log of zero or negative number is undefined<br /> echo &quot;----nvector d - fatal errorn&quot;;<br /> $d = scale_by_log($a, -2.5);</p> <p >?&gt;</p> <p >上例的输出类似于:</p> <p >vector a<br /> Array<br /> (<br /> [0] =&gt; 2<br /> [1] =&gt; 3<br /> [2] =&gt; foo<br /> [3] =&gt; 5.5<br /> [4] =&gt; 43.3<br /> [5] =&gt; 21.11<br /> )<br /> ----<br /> vector b - a warning (b = log(PI) * a)<br /> &lt;b&gt;WARNING&lt;/b&gt; [1024] Value at position 2 is not a number, using 0 (zero)&lt;br /&gt;<br /> Array<br /> (<br /> [0] =&gt; 2.2894597716988<br /> [1] =&gt; 3.4341896575482<br /> [2] =&gt; 0<br /> [3] =&gt; 6.2960143721717<br /> [4] =&gt; 49.566804057279<br /> [5] =&gt; 24.165247890281<br /> )<br /> ----<br /> vector c - an error<br /> &lt;b&gt;ERROR&lt;/b&gt; [512] Incorrect input vector, array of values expected&lt;br /&gt;<br /> NULL<br /> ----<br /> vector d - fatal error<br /> &lt;b&gt;FATAL&lt;/b&gt; [256] log(x) for x &lt;= 0 is undefined, you used: scale = -2.5&lt;br /&gt;<br /> Fatal error in line 36 of file trigger_error.php, PHP 4.0.2 (Linux)&lt;br /&gt;<br /> Aborting...&lt;br /&gt;<br /> <br /> </p> <p ></p>
T:0.004693s,M:240.61 KB
返回顶部 留言