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

How to customize error handling in php

王林
Release: 2023-03-05 09:16:01
Original
2363 people have browsed it

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);
Copy after login

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){
    函数内部写入处理错误的代码
}
Copy after login

(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");
 }
 ?>
Copy after login

Output result:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template