: This article mainly introduces common PHP function block _ errors and exception handling-php (32). For more information about PHP tutorials, see. I. handling errors and exceptions
1.1 error types and basic debugging methods
?? PHP program errors generally occur in the following three fields:
?? Syntax error:
?? Syntax errors are the most common and easy to fix. For example, a semicolon is missing in the code. Such errors will prevent script execution.
?? Runtime error:
?? This type of error generally does not stop PHP script execution, but does prevent the current task. An error is output, but the php script continues to be executed.
?? Logical error:
?? This type of error is the most troublesome. it neither prevents script execution nor outputs error messages.
?? An exception occurs when a program is executed, or an event that interrupts normal commands and jumps to other program modules for further execution.
PHP error reporting level
?? E_ALL // all information value: 6143
?? E_ERROR // fatal runtime error value: 1
?? E_RECOVERABLE_ERROR // near fatal runtime error. if it is not captured, it is regarded as E_ERROR value: 4096
?? E_WARNING // Runtime warning (non-fatal error) value: 2
?? E_PARSE // parsing error value at Compilation: 4
?? E_NOTICE // Runtime reminder (often a bug, or intentionally) value: 8
?? E_STRICT // code standardization warning (how to modify to forward compatibility) value: 2048
?? E_CORE_ERROR // critical error value during PHP initialization: 16
?? E_CORE_WARNING // WARNING (non-fatal error) value during PHP initialization at startup: 32
?? E_COMPILE_ERROR // fatal error value during compilation: 64
?? E_COMPILE_WARNING // The value of warning (non-fatal error) during compilation: 128
?? E_USER_ERROR // custom fatal error value: 256
?? E_USER_WARNING // user-defined warning (non-fatal error) value: 512
?? E_USER_NOTICE // user-defined reminder (often bug) value: 1024
Php. ini configuration file
Display_errors: whether to enable PHP to output error reports
?? Values: On (default output error report) and Off (all error messages are blocked)
?? In the PHP script, you can call the ini_set () function to dynamically set the php. ini configuration file.
?? For example: ini_set ("display_errors", "On"); // display all error messages
?? Error_reporting: set different error reporting levels.
?? Error_reporting = E_ALL &~ E_NOTICE
-- Can throw any non-attention error, default value
?? Error_reporting = E_ERROR | E_PARSE | E_CORE_ERROR
-- Only fatal runtime errors, new parsing errors, and core errors are considered.
?? Error_reporting = E_ALL &~ (E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE)
-- Reports all errors except user errors.
?? In the PHP script, you can dynamically set the error report level through the error_reporting () function. For example, error_reporting (E_ALL);]
Set error level instance: error. php
Test error report
PHP error reporting behavior configuration instructions
Display_startup_errors = Off
?? Whether to display PHP engine initialization errors.
?? Log_errors = On
?? Determines the location of log statement records.
?? Error_log (null by default)
?? Specify the file or log of the error written into the system log syslog.
?? Log_errors_max_len = 1024
?? The maximum length of each log entry, in bytes. 0 indicates the maximum value.
Error log 1.2
You can record error logs in either of the following ways:
?? Use the specified file to record error report logs
?? The error log is recorded in the operating system log.
Use the specified file to record error report logs
1. configure php. ini first:
?? Error_reporting = E_ALL // every error will be sent to PHP
?? Display_errors = Off // no error report is displayed
?? Log_errors = On // determines the location of log statement records.
?? Log_errors_max_log = 1024 // maximum length of each log entry
?? Error_log = G:/myerror. log // specify the file to which the error is written.
?? 2. use a function: use error_log () in the PHP file to record logs, and you can write the information to the myerror. log file.
?? For example, error_log ("login failed! ");
2. use four functions to record logs:
?? Define_syslog_variables (); // initialize the configuration for system logs.
?? Openlog (); // open a log link
?? Syslog (); // send a log
Example
View logs, for example, in windows, right-click my computer and choose Manage> System Tools> Event Viewer> application options to view the log.
1.3 Exception handling
Exception processing is used to change the normal process of the script when a specified error occurs. It is a new important feature in PHP5. Exception handling is a scalable and easy-to-maintain unified error handling mechanism, and provides a new object-oriented error handling method.
?? Exception handling format:
Try {
Use try to include code that may cause exceptions.
If an exception occurs, try to catch the exception and submit it to the catch.
Throw exception statement: throw exception object.
} Catch (exception object parameter ){
Handle exceptions here.
} [Catch (.,,) {
......
}]
The above describes common PHP function blocks _ errors and exception handling-php (32), including some content, hope to be helpful to friends who are interested in PHP tutorials.