PHP error handling, running error analysis, error level explanation, Error Processor, Custom Error Processor

Source: Internet
Author: User
PHP error handling, running error analysis, error level explanation, Error Processor, Custom Error Processor

PHP error learning notes PHP error handling

1) PHP errors

Syntax error: indicates a compilation error that cannot be recovered, indicating that a syntax problem occurs, causing the PHP interpreter to stop working.

Environment error: it refers to issues related to the script running environment and related services.

Programming errors: unexpected errors that occur during normal program execution

Logic error: The program is successfully executed but does not get the correct result. it is usually caused by programming problems.

There are two main categories: Standard PHP errors and exceptions.

2) error level: every error in PHP is associated with a severity or type of error.

Description of level integer values

E_ERROR 1 is a fatal error, indicating that PHP cannot be recovered from the error, which usually occurs at the operating system level, such as memory allocation.

The E_WARNING 2 warning indicates a non-fatal error, which can be recovered by PHP and is usually retained as an environmental error. if a script cannot be executed to access the database

E_PARSE 4 syntax errors during compilation

E_NOTICE 8 indicates that the script may have potential errors, such as accessing undefined variables and array indexes.

E_CORE_ERROR 16 fatal php kernel error, equivalent to E_ERROR, only occurs in the PHP kernel

E_CORE_WARNING 32 is a non-fatal PHP kernel error, which is equivalent to E_WARNING, but occurs in the PHP kernel.

An error occurs during compilation of E_COMPILE_ERROR 64, which is equivalent to E_ERROR, but occurs in the Zend Engine.

E_COMPILE_WARNING 128 non-fatal Zend Engine error

E_USER_ERROR 256 is equivalent to E_ERROR, which occurs in error handling set by trigger_error ().

E_USER_WARNING 512 is equivalent to E_WARNING and occurs in error handling set by trigger_error ().

E_USER_NOTICE 1024 is equivalent to E_NOTICE and occurs in error handling set by trigger_error ().

E_ALL 2047 includes errors except for the E_STRICT error level

E_STRICT 2048 is mainly a notification indicating codes that do not approve of use or best practices that are not followed by the PHP interpreter

3) PHP provides built-in settings for error handling

Error_reporing command: indicates which errors need to be reported

Display_error command: when display_error is set, PHP errors will be displayed in the script output in the embedded form, which is a good setting for development.

Log_errors and error_log commands: if log_errors is set, errors are recorded at the location specified by the error_log command.

Ignore_repeated_errors and ignore_repeated_source: prevents log files from being filled up when the same error is repeated.

4) trigger of an error

① PHP interpreter triggered

② Writing code triggers PHP errors

You can use the trigger_error function of PHP in the code to trigger an error.

Trigger_error ($ error_message, $ error_type = E_USER_NOTICE)
Function: use it in the code to trigger PHP errors.
Parameter: $ error_message error message. The maximum value is 1024 bytes. the part greater than 1024 is truncated. $ error_type is an error-level constant. this error level can only be an error of the E_USER type, including E_USER_ERROR, E_USER_WARNING, and E_USER_NOTICE. the default value is E_USER_NOTICE.
Returned value: Boolean. if the error $ error_type is specified, false is returned. otherwise, true is returned.

5) customize error processors in PHP
PHP allows developers to customize error processors. these error processors can adopt any valid PHP callback method: function, static class method, or dynamic class method.
The callback method must conform to the error processor prototype:
Function handler ($ errno, $ errmsg [, $ filename [, $ linenum [, $ vars])
The first parameter: an integer indicates the error level;
Second parameter: Error Description
Third parameter: file name of the code with an error
Fourth parameter: the code line number of the code in which the error occurs
Fifth parameter: contains an array of all variables in the range of triggering errors
Note: If the script execution is not explicitly exited in the error processor, the program execution will continue at the original trigger error location.
Use the set_error_handler function to tell PHP to use an Error Processor
Set_error_hander ($ error_handler [, $ error_type])
The first parameter is a callback function pointing to the error processor.
The second parameter is used to tell PHP which error type your custom function can handle.
By default, if no error type is provided, all errors except E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and E_STRICT will be handled. Because you can specify a type, you can specify multiple error processors, each of which is used to process one type.

6) record and report errors
The easiest way to record errors is to configure logging through php. ini.
Ini_set ('error _ log', 'C:/temp/error. log ')
Ini_set ('ignore _ repeated_errors ', true)
Ini_set ('ignore _ repeated_source ', true)
Ini_set ('log _ errors ', true)

The following is my PHP Custom Error Processor. due to my limited level and imperfect code, please forgive me!

 

 * @ Version $ Id: ErrorHandler. class. php, v 1.0 20:16:00 uw Exp * @ copyright©2011, xudianyang */class ErrorHandler {// error log file protected $ mLogfile = ''; // error message public $ mError_msg = ''; // the name of the wrong code file is public $ mFilename = ''; // the wrong code line number is public $ mLinenum = 0; // when an error occurs, the defined variable public $ mVars = array () in the memory; // The linefeed public $ mLF in the operating system; /*** constructor ** @ param string $ error_msg error message * @ param string $ file name with incorrect filename code * @ param int $ linenum code error line number * @ param array $ the variable re * @ return void */public function _ construct ($ error_msg, $ filename, $ linenum, $ vars = array (), $ logfile = '') {$ this-> mError_msg = $ error_msg; $ this-> mFilename = $ filename; $ this-> mLinenum = $ linenum; $ this-> mVars = $ vars; if (preg_match ('/WIN/I', PHP_ OS) {$ logfile. = "c:/WINDOWS/Temp/php /". trim ($ logfile, "/\"); $ this-> mLF = "\ r \ n";} else {$ logfile = "/tmp/php /". trim ($ logfile, "/\"); $ this-> mLF = "\ n" ;}$ this-> mLogfile = $ logfile ;} /*** Error Processor callback method ** @ param int $ errno error-level integer value * @ param int $ error_msg error message * @ param string $ filename code error file name *@ param int $ linenum code error line number * @ param array $ vars the variable defined in the memory */public static function handle ($ errno, $ error_msg, $ filename, $ linenum, $ vars) {$ logfile = 'Error. log'; $ errorhandler = new self ($ error_msg, $ filename, $ linenum, $ vars, $ logfile); switch ($ errno) {case E_USER_ERROR: return $ errorhandler-> handleError (); case E_USER_WARNING: case E_WARNING: return $ warning-> handleWarning (); case E_USER_NOTICE: case E_NOTICE: return $ errorhandler-> handleNotice (); default: return false;}/*** fatal error occurred during processing ** @ return void **/public function handleError () {$ backtrace = print_r (debug_backtrace (), true); $ error_time = date ("Y-m-d H: I: s l"); $ message = <
 
  
The mLF} application has a fatal error: {$ this-> mLF} error message: {$ this-> mError_msg}; {$ this-> mLF} File name: {$ this-> mFilename}; {$ this-> mLF} row number: {$ this-> mLinenum}; {$ this-> mLF} Backtrace: {$ backtrace }{$ this-> mLF} EOT; error_log ($ message, 3, $ this-> mLogfile, "Fatal error occurred {$ this-> mLF }"); exit (1);}/*** handle warning-level errors ** @ return boolean */public function handleWarning () {$ backtrace = print_r (debug_backtrace (), true ); $ error_time = date ("Y-m-d H: I: s l"); $ message = <
  
   
The mLF} application has a warning error: {$ this-> mLF} error message: {$ this-> mError_msg}; {$ this-> mLF} File name: {$ this-> mFilename}; {$ this-> mLF} row number: {$ this-> mLinenum}; {$ this-> mLF} Backtrace: {$ backtrace }{$ this-> mLF} EOT; return error_log ($ message, 3, $ this-> mLogfile, "Warning error occurred {$ this-> mLF}");}/*** handle attention-level errors ** @ return boolean */public function handleNotice () {$ backtrace = print_r (debug_backtrace (), true); $ error_time = date ("Y-m-d H: I: s l"); $ message = <
   
    
The mLF} application has a note-level error: {$ this-> mLF} error message: {$ this-> mError_msg}; {$ this-> mLF} File name: {$ this-> mFilename}; {$ this-> mLF} row number: {$ this-> mLinenum}; {$ this-> mLF} Backtrace: {$ backtrace }{$ this-> mLF} EOT; return error_log ($ message, 3, $ this-> mLogfile, "Notice error occured {$ this-> mLF}") ;}} set_error_handler (array ('errhandler', 'handle');?>
   
  
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.