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

怪我咯
Release: 2023-03-13 12:14:01
Original
1369 people have browsed it

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

Output:

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 PHP set_error_handler() function - moonlit1228's column - CSDN blog. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!