欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php自定义错误处理用法实例

程序员文章站 2022-05-13 21:52:10
本文实例讲述了php自定义错误处理用法。分享给大家供大家参考。具体如下:

本文实例讲述了php自定义错误处理用法。分享给大家供大家参考。具体如下:

<?php
error_reporting(e_all);
function errhandler($errorno, $errorstr, $errorfile, $errorline)
{
  $display = true;
  $notify = false;
  $halt_script = false;
  $error_msg = "<br>the $errorno error is occurring at $errorline in
  $errorfile<br>";
  switch($errorno)
  {
   case e_user_notice:
   case e_notice:
   $halt_script = false;
   $notify = true;
   $label = "<b>notice</b>";
   break;
   case e_user_warning:
   case e_warning:
   $halt_script = false;
   $notify = true;
   $label = "<b>warning</b>";
   break;
   case e_user_error:
   case e_error:
   $label = "<b>fatal error</b>";
   $notify=true;
   $halt_script = false;
   break;
   case e_parse:
   $label = "<b>parse error</b>";
   $notify=true;
   $halt_script = true;
   break;
   default:
   $label = "<b>unknown error</b>";
   break;
  }
  if($notify)
  {
   $msg = $label . $error_msg;
   echo $msg;
  }
  if($halt_script) exit -1;
}
$error_handler = set_error_handler("errhandler");
echo "<br><h2>using custom error handler</h2><br>";
trigger_error("<br>error caused by e_user_notice</br>", e_user_notice);
trigger_error("<br>error caused by e_user_warning</br>", e_user_warning);
trigger_error("<br>error caused by e_user_error</br>", e_user_error);
trigger_error("<br>error caused by e_parse</br>", e_parse);
?>

希望本文所述对大家的php程序设计有所帮助。