The problem with the set_error_handle and at (@) symbols is that I use set_error_handle to write some system errors or warning information to the log file. But for some APIs such as fopen, if the file does not exist, it will also go to the self-written error handler function, [email & #160; protected], do not display such issues. [Email & #160; question about the prot set_error_handle and at (@) symbols
The problem is that I use set_error_handle to write some system errors or warnings to the log file.
But for some APIs such as fopen, if the file does not exist, it will also go to the self-written error handler function, [email protected], and will not display such problems.
[Email protected] is removed. How can this problem be solved?
Thank you!
------ Solution --------------------
Capture with try catch
------ Solution --------------------
You can remove it. to use set_error_handler, you only need to pay attention to two points:
1. set_error_handler only captures the error type defined in error_reporting. this can be configured in php. ini or use function settings;
2. if the returned value of the custom error handling function is false, the default php error handling will be triggered. If the returned value is true, the default php error handling will not be triggered, and the annoying error cannot be seen.
For the writing method, refer.
Function myErrorHandler ($ errno, $ errstr, $ errfile, $ errline)
{
If (! (Error_reporting () & $ errno )){
// If this error type is not included in error_reporting
Return;
}
Switch ($ errno ){
Case E_USER_ERROR:
Echo"
My ERROR[$ Errno] $ errstr
\ N ";
Echo "Fatal error on line $ errline in file $ errfile ";
Echo ", PHP". PHP_VERSION. "(". PHP_ OS .")
\ N ";
Echo "Aborting...
\ N ";
Exit (1 );
Break;
Case E_USER_WARNING:
Echo"
My WARNING[$ Errno] $ errstr
\ N ";
Break;
Case E_USER_NOTICE:
Echo"
My NOTICE[$ Errno] $ errstr
\ N ";
Break;
Default:
Echo "Unknown error type: [$ errno] $ errstr
\ N ";
Break;
}
// If false is returned, the default php error processing is triggered. if true is returned, only custom functions are used for processing.
Return true;
}
Set_error_handler ('myerrorhandler ');
Fopen ('/tmp/Foo', 'r ');
------ Solution --------------------
Tested, #5 code can meet your requirements!
After execution, if
Fopen ('/tmp/Foo', 'r ');
Failed, displayed
Unknown error type: [2] fopen (/tmp/foo) [function. fopen]: failed to open stream: No such file or directory
If
@ Fopen ('/tmp/Foo', 'r ');
Failed, no display