PHP error handl...LOGIN

PHP error handling custom error handling function

The starting point of this piece of knowledge is a bit high. Most people have no experience in software engineering or custom error handling, and it is difficult to imagine usage scenarios. If you want to skip this block of learning, you can, and we support it.

This knowledge point does not have many practical application scenarios. If you have plans to start writing your own framework, or if you have completed the first project of this book.

You can go back and read the contents of chapter 11.4.

Two functions commonly used for user-defined errors:

set_error_handler (callable $callback error handling function)
Set a user-defined error handling function

trigger_error (string $error_msg)
Generate a user-level error/warning/notice message

<?php
//定义一个自定义的错误处理函数
function customError($errno, $errstr, $errfile, $errline) {
   //输出错误消息
   echo "<b>Custom error:</b> [$errno] $errstr<br />";
   //输出错误文件和错误行
   echo "Error on line $errline in $errfile<br />";
   echo "Ending Script";
   //中止程序运行
   exit;
}

//使用set_error_handler 绑定用户自定义函数
set_error_handler("customError");


$test=2;

//触发自定义错误
if ($test > 1) {
   trigger_error("A custom error has been triggered");
}
?>


Next Section
<?php //定义一个自定义的错误处理函数 function customError($errno, $errstr, $errfile, $errline) { //输出错误消息 echo "<b>Custom error:</b> [$errno] $errstr<br />"; //输出错误文件和错误行 echo "Error on line $errline in $errfile<br />"; echo "Ending Script"; //中止程序运行 exit; } //使用set_error_handler 绑定用户自定义函数 set_error_handler("customError"); $test=2; //触发自定义错误 if ($test > 1) { trigger_error("A custom error has been triggered"); } ?>
submitReset Code
ChapterCourseware