Home > Article > Backend Development > PHP custom error handling function_PHP tutorial
In PHP development, we generally use PHP's built-in error handling method to handle some errors, but for some we need to customize some error handling mechanisms to solve problems that the system's built-in system cannot solve.
Basic error handling: use the die() function
The first example shows a simple script that opens a text file:
The code is as follows | Copy code |
代码如下 | 复制代码 |
$file=fopen("welcome.txt","r"); ?> |
?>
If the file does not exist, you will get an error like this:
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:
代码如下 | 复制代码 |
if(!file_exists("welcome.txt")) |
The code is as follows | Copy code |
if(!file_exists("welcome.txt"))<🎜> {<🎜> die("File not found");<🎜> }<🎜> else<🎜> {<🎜> $file=fopen("welcome.txt","r");<🎜> }<🎜> ?> |
Now, if the file does not exist, you will get an error message like this:
File not found The above code is more efficient than the previous code because it uses a simple error handling mechanism to terminate the script after the error.
However, simply terminating the script is not always appropriate. Let's examine alternative PHP functions for handling errors.
Let’s look at a custom error handling function
The code is as follows | Copy code | ||||||||
If(!(error_reporting() &$errno)){return;} switch ($errno){ case E_USER_ERROR: echo "My ERROR [$errno] $errstr "; echo "Error line: $errline in file: $errfile "; echo " PHP version: " .PHP_VERSION ." (" .PHP_OS .") "; break; case E_USER_WARNING: echo "My WARNING [$errno] $errstr "; break; case E_USER_NOTICE: echo "My NOTICE [$errno] $errstr "; break; default: echo "Unknown error type: [$errno] $errstr "; break; } Return true; } function trigger_test($age){//Test function that throws an error If($age <= 0 || $age > 999) trigger_error("Illegal age: $age years old", E_USER_ERROR); If($age < 18) trigger_error("Underage: $age years old", E_USER_WARNING); If($age > 40 && $age < 100) trigger_error("Slightly older: $age years old", E_USER_NOTICE); } //If you just handle errors simply and uniformly: $errorHandler = set_error_handler("myErrorHandler"); trigger_test(1000);//will throw an error level error
function myError($errno, $errstr, $errfile, $errline){ Print_r(func_get_args()); //Specific processing method } function myWarning($errno, $errstr, $errfile, $errline){ Print_r(func_get_args()); //Specific processing method } function myNtice($errno, $errstr, $errfile, $errline){ Print_r(func_get_args()); //Specific processing method } |
trigger_error('Intentionally throwing an error, or a serious one!', E_USER_ERROR);
Attached below are some detailed explanations of some PHP error codes
Parameters | Description |
---|
参数 | 描述 |
---|---|
error_level |
必需。为用户定义的错误规定错误报告级别。必须是一个值数。 参见下面的表格:错误报告级别。 |
error_message | 必需。为用户定义的错误规定错误消息。 |
error_file | 可选。规定错误在其中发生的文件名。 |
error_line | 可选。规定错误发生的行号。 |
error_context | 可选。规定一个数组,包含了当错误发生时在用的每个变量以及它们的值。 |
Required. Specifies the error reporting level for user-defined errors. Must be a value.
值 | 常量 | 描述 |
---|---|---|
2 | E_WARNING | 非致命的 run-time 错误。不暂停脚本执行。 |
8 | E_NOTICE |
Run-time 通知。 脚本发现可能有错误发生,但也可能在脚本正常运行时发生。 |
256 | E_USER_ERROR | 致命的用户生成的错误。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_ERROR。 |
512 | E_USER_WARNING | 非致命的用户生成的警告。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_WARNING。 |
1024 | E_USER_NOTICE | 用户生成的通知。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_NOTICE。 |
4096 | E_RECOVERABLE_ERROR | 可捕获的致命错误。类似 E_ERROR,但可被用户定义的处理程序捕获。(参见 set_error_handler()) |
8191 | E_ALL |
所有错误和警告,除级别 E_STRICT 以外。 (在 PHP 6.0,E_STRICT 是 E_ALL 的一部分) |