Home  >  Article  >  Backend Development  >  Detailed explanation of error error level in php

Detailed explanation of error error level in php

PHPz
PHPzOriginal
2023-04-04 14:29:211093browse

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:

  1. E_ERROR: Serious error, the system cannot continue execution and needs to stop execution immediately.
  2. E_WARNING: Warning level error, the system can continue to execute.
  3. E_NOTICE: Notification level error, some values ​​were used before assignment.
  4. E_STRICT: Tips for coding standards, used to mark some irregularities in code writing.
  5. E_DEPRECATED: A reminder of obsolete functionality that will be removed in a future version.
  6. E_PARSE: Syntax analysis error, the code cannot be parsed.

Now, let’s understand these error levels one by one.

  1. E_ERROR

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);
}
  1. E_WARNING

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());
}
  1. E_NOTICE

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('未定义的变量');
}
  1. E_STRICT

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.

  1. E_DEPRECATED

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.

  1. E_PARSE

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!

Statement:
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