Home >Backend Development >PHP Problem >How to customize error handling in php

How to customize error handling in php

王林
王林Original
2020-08-26 13:59:322359browse

php custom error handling method: You can use the set_error_handler() function to achieve this. This function can set a user-defined error handling function, such as [set_error_handler("my_define_error")].

How to customize error handling in php

Custom error handling:

When an error occurs, we handle it ourselves and set an error handling function.

(Video tutorial recommendation: php video tutorial)

set_error_handler() function sets the user-defined error handling function.

Syntax:

set_error_handler(errorhandler,E_ALL|E_STRICT);

Parameters:

  • errorhandler Required. Specifies the name of the user error handling function.

  • E_ALL|E_STRICT Optional. Specifies what error reporting level is displayed for user-defined errors. The default is "E_ALL".

##For example: set_error_handler("my_define_error");

Then define the function and set the error handling in detail in the function.

Grammar:

function my_define_error(errCode,errCode,errCode,errMsg,errFile,errFile,errFile,errLine){
    函数内部写入处理错误的代码
}

(Related tutorial recommendations:

php graphic tutorial)

Note: The order of this formal parameter is specified, and is The function will be called uniformly by the system and the actual parameter data will be transmitted.

Note: Custom errors can only handle "non-fatal errors", which means they cannot handle E_ERROR errors.


Code implementation:

<?php // 用户定义的错误处理函数
 function myErrorHandler($errno, $errstr, $errfile, $errline) {
     echo "<b>Custom error:</b> [$errno] $errstr<br>";
     echo " Error on line $errline in $errfile<br>";
 } // 设置用户定义的错误处理函数
 set_error_handler("myErrorHandler");

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

Output result:

Custom error: [1024] A custom error has been triggered
 Error on line 14 in C:\webfolder\test.php

The above is the detailed content of How to customize error handling in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn