<p >set_error_handler<br />
(PHP 4 >= 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 ><?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 "<b>My ERROR</b> [$errno] $errstr<br />n";<br />
echo " Fatal error in line $errline of file $errfile";<br />
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />n";<br />
echo "Aborting...<br />n";<br />
exit(1);<br />
break;<br />
case E_USER_WARNING:<br />
echo "<b>My WARNING</b> [$errno] $errstr<br />n";<br />
break;<br />
case E_USER_NOTICE:<br />
echo "<b>My NOTICE</b> [$errno] $errstr<br />n";<br />
break;<br />
default:<br />
echo "Unkown error type: [$errno] $errstr<br />n";<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 <= 0) {<br />
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);<br />
}</p>
<p > if (!is_array($vect)) {<br />
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);<br />
return null;<br />
}</p>
<p > for ($i=0; $i<count($vect); $i++) {<br />
if (!is_numeric($vect[$i]))<br />
trigger_error("Value at position $i is not a number, using 0 (zero)", 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("myErrorHandler");</p>
<p >// trigger some errors, first define a mixed array with a non-numeric item<br />
echo "vector an";<br />
$a = array(2,3, "foo", 5.5, 43.3, 21.11);<br />
print_r($a);</p>
<p >// now generate second array, generating a warning<br />
echo "----nvector b - a warning (b = log(PI) * a)n";<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 "----nvector c - an errorn";<br />
$c = scale_by_log("not array", 2.3);<br />
var_dump($c);</p>
<p >// this is a critical error, log of zero or negative number is undefined<br />
echo "----nvector d - fatal errorn";<br />
$d = scale_by_log($a, -2.5);</p>
<p >?></p>
<p >上例的输出类似于:</p>
<p >vector a<br />
Array<br />
(<br />
[0] => 2<br />
[1] => 3<br />
[2] => foo<br />
[3] => 5.5<br />
[4] => 43.3<br />
[5] => 21.11<br />
)<br />
----<br />
vector b - a warning (b = log(PI) * a)<br />
<b>WARNING</b> [1024] Value at position 2 is not a number, using 0 (zero)<br /><br />
Array<br />
(<br />
[0] => 2.2894597716988<br />
[1] => 3.4341896575482<br />
[2] => 0<br />
[3] => 6.2960143721717<br />
[4] => 49.566804057279<br />
[5] => 24.165247890281<br />
)<br />
----<br />
vector c - an error<br />
<b>ERROR</b> [512] Incorrect input vector, array of values expected<br /><br />
NULL<br />
----<br />
vector d - fatal error<br />
<b>FATAL</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br /><br />
Fatal error in line 36 of file trigger_error.php, PHP 4.0.2 (Linux)<br /><br />
Aborting...<br /><br />
<br />
</p>
<p ></p>