Home  >  Article  >  Backend Development  >  What are the types of php error levels?

What are the types of php error levels?

青灯夜游
青灯夜游Original
2023-01-30 16:14:383266browse

php error level types: 1. Parsing error or syntax error. This error will stop the execution of the program and display an error message; 2. Fatal error; 3. Warning error, which means that the syntax of the program has no errors. , but during the execution process, some unreasonable aspects of the program are found, thus prompting a warning message, and the program will continue to execute; 4. Pay attention to errors, similar to warning errors, which means that the program contains errors, but it Allow script execution; 5. The lowest level error (not recommended, not recommended) will occur when using some expired functions, and the program will continue to execute.

What are the types of php error levels?

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

1. PHP Error type

PHP error level
Parse error > Fatal Error > Waning > Notice > Deprecated

1. Parse error or syntax error (Parse error)

Syntax error is the most common error in programming and the easiest to solve. For example: when a semicolon is missing, An error message will be displayed. This error stops program execution and displays an error message. We can correct the program based on the error message and re-execute it.

[Example] The following uses simple code to demonstrate common syntax errors and related error messages.

<?php
    $a = 1;
    $b = 2;
    $c = $a + $b
    echo ;
?>

; is omitted at the end of line 4 in the above code, so running the above code will display the following error message:

Parse error: syntax error, unexpected 'echo' (T_ECHO) in D:\WWW\index.php on line 5

As can be seen from the above example and running results, syntax errors will Prevents the program from continuing downward execution. Only after these errors are resolved can the program execute smoothly.

2. Fatal error:

This is the PHP compiler that understands the PHP code but it recognizes Error type for undeclared function. This means calling a function without a function definition.
The program reports an error directly and the code needs to be modified! ! ! To interrupt program execution, you can use the register_shutdown_function() function to trigger a function before the program terminates

For example

<?php 
function add($x, $y) { 
    $sum = $x + $y; 
    echo "sum = " . $sum; 
}
$x = 0; 
$y = 20;
add($x, $y); 

diff($x, $y); 
?>

What are the types of php error levels?

Explanation: In line 10, the call Function diff(), but function diff() is not defined in the declaration, so it gives error.

3. Warning:

There is no syntax error in the program, but during execution , PHP will find something unreasonable in the program and prompt a warning message, but the program will continue to execute.

Example: When using 0 as the divisor, it will cause the program to run incorrectly and output an error message.

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

Error:

What are the types of php error levels?

##4. Notice:

It is similar to a warning error, which means that the program contains an error, but it allows the script to be executed. It will occur when using some undefined variables, constants or array keys without quotation marks, and the program will continue to execute

<?php   
header("content-type:text/html;charset=utf-8");
$x = "PHP中文网"; 
echo $x; 
echo $y; 
?>

What are the types of php error levels?

Explanation: This program uses the undeclared variable $y, Hence it gives error message.

5. The lowest level error (Deprecated, not recommended, not recommended)

Using some expired functions The time will appear and the program will continue to execute.

2. PHP incorrect configuration

1. Modify the PHP configuration file php.ini

error_reporting = E_ALL&~E_NOTICE; //设置错误报告级别
display_errors = 1; //开发环境开启,生产环境关闭

2. Use the error_reporting() function

After setting in this way, it can take effect immediately. But only in the area behind the error_reporting() function call in the current script.

int error_reporting ([ int $level ] )1.

The parameter can be an integer or the corresponding constant identifier. It is recommended to use the constant form. The return value is the value (integer value) of the error reporting level in effect at the current location.


Note: The default value of display_errors in the configuration file php.ini is On, which means error prompts are displayed. If set to Off, all error prompts will be turned off.

Use error_reporting(0) or add

@ in front of the function to suppress error output to prevent error messages from leaking sensitive information.

Parameter level

New error_reporting level. Can be a bitmask or a named constant.

It is recommended to use named constants to ensure compatibility with future versions. Due to the addition of error levels and the increase in the range of integer values,

older integer-based error levels will not always behave as expected.

level possible values ​​(error reporting level in php)

Common: about 15 in total

What are the types of php error levels?

15个正好对应二进制的15位。

需要注意的是, 上述表中的字段, 不是一成不变的, 不同的PHP版本, 值可能会不同

任意数目的以上选项都可以用“或”来连接(用 OR 或 |),这样可以报告所有需要的各级别错误。

例如,下面的代码关闭了用户自定义的错误和警告,执行了某些操作,然后恢复到原始的报错级别:

例:

<?php
     error_reporting(0);                //禁用错误报告
     error_reporting(E_ERROR | E_WARNING | E_PARSE);//报告运行时错误
     error_reporting(E_ALL);            //报告所有错误
     error_reporting(E_ALL ^ E_NOTICE); //除E_NOTICE报告所有错误,是在php.ini的默认设置
     error_reporting(-1);               //报告所有 PHP 错误
     error_reporting(3);                //不报E_NOTICE
     error_reporting(11);               //报告所有错误
     ini_set(&#39;error_reporting&#39;, E_ALL); // 和 error_reporting(E_ALL); 一样
     error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);//表示php错误,警告,语法错误,提醒都返错。
?>

三、PHP异常

PHP的异常是PHP5的新增特性,与JAVA/C#的异常不同,PHP异常需要手动抛出throw new Exception,而不是系统自动抛出。

PHP错误与异常的区别,他们是2个不同的概念,但有共同的地方:
如果异常不捕获处理,程序将会终止,并报出Fatal Error 错误,看到这里大家就会觉得异常是不是错误的一种,这是一种错觉,但这样理解也可以。但异常捕获后程序可以继续执行,而真正的Fatal Error错误出现后程序就必须终止。

异常可以使用 try{}catch(){} 来捕获捕获,捕获之后后续代码可以继续执行;而错误是无法使用 try{}catch(){} 捕获的。
如果抛出了异常,就必须捕获它,否则程序终止执行。

推荐学习:《PHP视频教程

The above is the detailed content of What are the types of php error levels?. 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