&reftitle.examples; 下面我们可以看到错误处理功能在 PHP 中的使用的示例。定义错误处理函数,记录错误信息到文件中(使用 XML 格式),并在发生严重错误时向开发人员发送电子邮件。 在脚本中使用错误句柄 'Error', E_WARNING => 'Warning', E_PARSE => 'Parsing Error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compile Error', E_COMPILE_WARNING => 'Compile Warning', E_USER_ERROR => 'User Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_STRICT => 'Runtime Notice', E_RECOVERABLE_ERROR => 'Catchable Fatal Error' ); // 设置要保存变量跟踪信息的错误类别 $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE); $err = "\n"; $err .= "\t" . $dt . "\n"; $err .= "\t" . $errno . "\n"; $err .= "\t" . $errortype[$errno] . "\n"; $err .= "\t" . $errmsg . "\n"; $err .= "\t" . $filename . "\n"; $err .= "\t" . $linenum . "\n"; if (in_array($errno, $user_errors)) { $err .= "\t" . wddx_serialize_value($vars, "Variables") . "\n"; } $err .= "\n\n"; // for testing // echo $err; // 记录错误信息到错误日志,并在发生关键用户错误时发送电子邮件 error_log($err, 3, "/usr/local/php4/error.log"); if ($errno == E_USER_ERROR) { mail("phpdev@example.com", "Critical User Error", $err); } } function distance($vect1, $vect2) { if (!is_array($vect1) || !is_array($vect2)) { trigger_error("Incorrect parameters, arrays expected", E_USER_ERROR); return NULL; } if (count($vect1) != count($vect2)) { trigger_error("Vectors need to be of the same size", E_USER_ERROR); return NULL; } for ($i=0; $i ]]>