Home > Article > Backend Development > Detailed explanation of error error level in php
When developing with PHP, you often need to use the logging function. PHP provides the error_log function, which can save error logs to a local or remote server. The error log is divided into different error levels, including:
Now, let’s understand these error levels one by one.
The E_ERROR error level is the highest level error in PHP and usually causes the PHP program to be unable to continue running. For example, an undefined variable, calling a non-existent method or function, etc. This level of error usually needs to be handled using the trigger_error or set_error_handler function.
The following is a code example that uses the trigger_error function to record error logs:
if(!function_exists('test')){ trigger_error('函数test不存在', E_USER_ERROR); }
E_WARNING error level usually means there are some minor problems in the code , will not prevent the PHP program from continuing to run. For example, calling a class or method that does not exist, calling a method that is not accessible, etc. Errors of this level can usually be handled using try...catch statements.
The following is a code example that uses try...catch to capture warning errors:
try{ $test = new Test(); } catch (Error $e) { error_log($e->getMessage()); }
E_NOTICE error level is usually some important The information was not processed correctly. For example, using uninitialized variables, operating on undefined constants, etc. Errors of this level can usually be handled by using the isset function.
The following is a code example that uses the isset function to handle notification errors:
if (!isset($test)) { error_log('未定义的变量'); }
The E_STRICT error level is usually used to prompt PHP programmers Write irregular code. For example, using deprecated syntax, inappropriate type hints, etc. Errors of this level can be resolved by modifying the code.
The E_DEPRECATED error level is usually used to remind PHP programmers that certain features will be removed in future versions. For example, the parameters of a certain function have changed but the old version of the function parameters are still used. Errors of this level can be resolved by modifying the code.
The E_PARSE error level is a syntax analysis error. A syntax error is detected during the correctness check of the PHP program. Usually the program cannot be parsed and executed. For example, forgetting to enter a semicolon at the end of a statement, using invalid keywords, etc. Errors of this level can be resolved by modifying the code.
When writing PHP programs, we should handle various error levels reasonably and record error information in a timely manner to ensure the robustness and stability of the program.
The above is the detailed content of Detailed explanation of error error level in php. For more information, please follow other related articles on the PHP Chinese website!