Home > Article > Backend Development > What are the common error types in php
Common error types in php are: 1. Parse error type, indicating parsing error or syntax error; 2. Fatalerror type, indicating fatal error; 3. Warning type, indicating warning error; 4. Notice type, Indicates attention to errors; 5. Deprecated type, indicating the lowest level error.
Operating system for this tutorial: Windows 10 system, PHP version 8.1, 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, such as: missing a point An error message will be displayed when the number is reached. 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 type of error where the PHP compiler understands the PHP code but it recognizes an 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, such as
<?php function add($x, $y) { $sum = $x + $y; echo "sum = " . $sum; } $x = 0; $y = 20; add($x, $y); diff($x, $y); ?>
Description: In line 10, the function diff() is called, But the function diff() is not defined in the declaration, so it gives an error.
3. Warning:
There is no syntax error in the program, but during execution, PHP will find some unreasonable aspects of the program, thus A warning message will appear, but the program will continue to execute.
Example: Using 0 as the divisor 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:
4. Note:
It is similar to a warning error, This means that the program contains an error, but it allows script execution. This occurs when some undefined variables, constants, or array keys are used without quotes, and the program continues execution
<?php header("content-type:text/html;charset=utf-8"); $x = "PHP中文网"; echo $x; echo $y; ?>
Description: This program uses the undeclared variable $y, so it gives an error message.
5. The lowest level error (Deprecated, not recommended, not recommended)
Will occur when using some expired functions, and the program will continue to execute.
2. PHP error configuration
In addition to setting it in the script, you can also configure it in the php.ini configuration file
error_reporting = E_ALL&~E_NOTICE; //设置错误报告级别 display_errors = 1; //开发环境开启,生产环境关闭
3 , PHP exception
PHP exception is a new feature of PHP5. Different from JAVA/C# exception, PHP exception needs to be thrown manually by throw new Exception instead of automatically thrown by the system.
The difference between PHP errors and exceptions. They are two different concepts, but they have something in common:
If the exception is not caught and handled, the program will terminate and a Fatal Error will be reported. , when you see this, everyone will wonder if the anomaly is a wrong kind. This is an illusion, but it can be understood this way. However, the program can continue to execute after the exception is caught, but the program must terminate after the real Fatal Error occurs.
Exceptions can be captured using try{}catch(){}
. After capture, subsequent code can continue to execute; while errors cannot be captured using try{}catch(){ }
Captured.
If an exception is thrown, it must be caught, otherwise the program will terminate execution.
4. PHP exception and error throwing
Exception throwing: throw new Exception('Some Error Message');
Error thrown: trigger_error();
trigger_error()
The triggered error will not be caught by the try-catch exception capture statement
5. PHP error handling
set_error_handler()
can only handle three levels of errors: Deprecated, Notice, and Waning, and after processing, the script will continue to execute the next line where the error occurred.
register_shutdown_function()
This method is the last callback function before the end of the script, so whether it is die()/error (exception)/or the script ends normally, it will be called
6. PHP exception Handling
set_exception_handler()
Set the default exception handler. If there is a try/catch capture, this function will not be executed. Otherwise, it will be executed. And if it is executed, the script will not continue to execute after the exception occurs. One line of code, the program will terminate immediately
set_exception_handler()
Notes
set_exception_handler(“myException”)
不仅可以接受函数名,还可以接受类的方法(公开的静态方法 及 公开的非静态方法 都可以),但需要以 数组形式 传递,数组的第一值为“类名”,第二个参数为“方法名”,如下代码所示:
<?php class App{ function myException($exception) { echo "<b>Exception:</b> " , $exception->getMessage(); } } set_exception_handler(array('App','myException')); throw new Exception('Uncaught Exception occurred'); ?>
七、PHP7 异常处理的大变化
在PHP7之前,Deprecated、Notice、Waning这类错误是可以捕获的(使用set_error_handler()
函数),而Fatel Error无法捕获的。
在PHP7之后,出现了一个异常与错误通用的接口Throwable,Exception类与Error类都实现了该接口,导致Error类或Error类的派生类的错误对象(大部分Fatel Error,而之前三类错误不变)也可以像Exception一样被捕获(2种捕获方法:1、try/catch 2、set_exception_handler())。
The above is the detailed content of What are the common error types in php. For more information, please follow other related articles on the PHP Chinese website!