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 //定義一個自定義的錯誤處理函數(shù) 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 綁定用戶自定義函數(shù) set_error_handler("customError"); $test=2; //觸發(fā)自定義錯誤 if ($test > 1) { trigger_error("A custom error has been triggered"); } ?>