Home  >  Article  >  Backend Development  >  PHP set_error_handler() function - moonlit1228's column - CSDN blog

PHP set_error_handler() function - moonlit1228's column - CSDN blog

怪我咯
怪我咯Original
2017-07-10 14:54:271424browse

set_error_handler() FunctionSet user-defined error handling function.

This function is used to create the user's own error handling method during runtime.

This function will return the old error handler, or null if it fails.

Syntax

set_error_handler(error_function,error_types)
Parameters Description
error_function Required. Specifies the function to run when an error occurs.
error_types Optional. Specifies at which error reporting level user-defined errors will be displayed. The default is "E_ALL".

Tips and Comments

Tips: If this function is used, the standard PHP error handling function will be completely bypassed. If Required, the user-defined error handler must terminate (die() ) the script.

Note: If an error occurs before the script is executed, the custom error handler will not be used because the custom program has not been registered at that time.

Example

Set a user-defined error handler through the set_error_handler() function, and then trigger the error (through trigger_error()):

<?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:

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 PHP set_error_handler() function - moonlit1228's column - CSDN blog. 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