Home >Backend Development >PHP Tutorial >Take you to understand the error types and error levels of PHP

Take you to understand the error types and error levels of PHP

WBOY
WBOYOriginal
2021-10-20 18:34:173284browse

In the previous article, I brought you "Five minutes to help you understand what exception handling is in PHP", which introduced in detail the issues related to PHP exception handling. This article In this article, let’s continue to take a look at the relevant knowledge of PHP error handling, PHP error types and PHP error levels. I hope it will be helpful to everyone!

Take you to understand the error types and error levels of PHP

It is inevitable that programmers will make some mistakes during the development process, or errors may occur due to other reasons. At this time, it is necessary for us to understand what is in PHP Error types and error levels. You can also click on the free "php Error Handling" teaching video to learn related knowledge.

PHP error types

PHP program errors are generally divided into three categories, which are syntax errors, execution errors and logic errors.

  • Grammar errors

Grammar errors are the most common in programming This is one of the easiest errors to fix. For example, an error message is displayed when a semicolon is missing. This error stops program execution and displays an error message. We can correct the program based on the error message and re-execute it.

Grammar errors can be said to be the most common type of error we encounter in the programming process. At the same time, I think it is also the easiest error to solve; for example: forgetting to enter a semicolon when typing. Or the function syntax is wrong.

<?php
    $a = 1
    echo $a;
?>

In the above example, there is a semicolon missing at the end of the second line, and the output result is incorrect.

After adding the semicolon, the program runs normally and the output result is 1. From this we can see that when a syntax error occurs, the program will stop and continue to execute. When the syntax error is resolved, the program can continue to execute.

  • Run-time error

Run-time error is the process of running the program An error occurs in the program. What needs to be noted at this time is that there is no syntax error, but during the execution process, PHP will warn you that there is something unreasonable in the program. Unlike syntax errors, the program will continue to run downwards.

The example is as follows:

<?php
    $a = 1;
    $b = 0;
    $c = $a / $b;
    echo "$a / $b = $c";
?>

In the above example, there is no syntax error, but 0 cannot be used as a divisor, and the output result is:

Take you to understand the error types and error levels of PHP

  • Logic error

The program with logic error will execute normally, but the output The result was wrong. The root cause occurred in the code we wrote. There was no alarm message during the execution of the program. The example is as follows:

<?php
    $a = 1;
    $b = 2;
    if($a = $b){
        echo &#39;$a = $b&#39;;
    }else{
        echo &#39;$a != $b&#39;;
    }
?>

Output result:

Take you to understand the error types and error levels of PHP

We can see from the above example that the output result is obviously wrong and illogical. , when a logic error occurs, there is no alarm message. This requires us to be more vigilant and avoid logical errors.

Expand knowledge: PHP error levels

There are both error types and error levels defined in PHP. The error levels can be defined in php.ini. (Click "How to upload files in PHP? You will understand after reading this!" to see how to find php.ini)

The error types in PHP are as follows:

1 ---E_ERROR---A fatal runtime error is generally an unrecoverable situation, such as a problem caused by memory allocation. The consequence is that the script terminates and no longer runs.

2 --- E_WARNING --- Runtime warning (non-fatal error), only prompt information is given, but the script will not terminate the operation.

4 --- E_PARSE --- Compile-time syntax parsing error, generated only by the analyzer.

8 --- E_NOTICE ---Runtime notification, indicating that the script encounters a situation that may appear as an error, but there may also be similar errors in scripts that can run normally. notify.

16 --- E_CORE_ERROR --- A fatal error that occurs during the PHP initialization startup process, similar to E_ERROR, but generated by the PHP engine core.​ ​ ​

64 ​ --- ​ E_COMPILE_ERROR ​ --- Fatal compile-time error, similar to E_ERROR, but generated by the Zend script engine.

128 --- E_COMPILE_WARNING --- Compile time warning (non-fatal error), similar to E_WARNING, but generated by the Zend script engine. .

1024 --- E_STRICT --- Enable PHP's suggestions for code modifications to ensure the best interoperability and forward compatibility of the code.​ ​ ​

8192 ​ --- ​ E_DEPRECATED ​ --- Runtime notification, when enabled, will give a warning for code that may not work properly in future versions.

16384 --- E_USER_DEPRECATED --- The warning message generated by the user is similar to E_DEPRECATED, but it is generated by the user himself using the PHP function trigger_error() in the code.

30719 --- E_ALL --- All error and warning messages except E_STRICT.​

If you want to know more about PHP, you can click on "PHP Video Tutorial" to learn!

The above is the detailed content of Take you to understand the error types and error levels of 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