PHP error handling error reporting level
Error report level
Error type
php Everyone’s most common error display screenshots, this book helps you summarize:
[Mastery level Error Type] We divide the most commonly used errors into three types:
Error Type | Description |
---|---|
E_ERROR | Error, the file is directly interrupted |
E_WARNING | Warning, the problem is serious. But it will continue to run |
E_NOTICE | prompts that some minor problems will not affect the program. Syntax parsing errors often occur when the project does not define |
E_PARSE | . Parsing errors are generated only by the parser. |
E_ALL | All errors |
E_STRICT | Enable PHP suggestions for code modifications to Ensure code has optimal interoperability and forward compatibility. |
E_DEPRECATED | When enabled, a warning will be given about code that may not work properly in future versions. |
Among the above types:
error is the most serious and must be resolved. Otherwise, the program cannot continue to execute.
warning is also very important. Tong also must be solved. If it is clear and intentional, there is no need to deal with it.
notice You can ignore it. But in some companies, project standards are particularly high. It must also be solved in projects with high standard requirements. Because notice will affect the execution efficiency of PHP. Usually happens when function is undefined etc.
Parse errors refer to grammatical errors and typographical errors, which must be resolved
Represents all errors of all types
[Understand the level of error types] From these three types, there are some other error items that need to be understood:
Error type | Error description |
---|---|
E_CORE_ERROR | A fatal error occurred during PHP initialization startup. This error is similar to E_ERROR, but is a warning (non-fatal error) generated by the PHP engine core |
E_CORE_WARNING | during PHP initialization startup. Similar to E_WARNING, but generated by the PHP engine core. |
E_COMPILE_ERROR | Fatal compile-time error. Similar to E_ERROR, but generated by the Zend script engine. |
E_COMPILE_WARNING | Compile-time warning (non-fatal error). Similar to E_WARNING, but generated by the Zend script engine |
E_USER_ERROR | User-defined error |
E_USER_WARNING | User-defined warning |
E_USER_NOTICE | User-defined prompt |
E_USER_DEPRECATED | User Low yield warning message. Similar to E_DEPRECATED, but generated by the user using the PHP function trigger_error() in the code. |
E_RECOVERABLE_ERROR | A fatal error that can be caught. It indicates that a potentially dangerous error has occurred, but has not caused the PHP engine to become unstable. |
In the learning process, just understand the above types. Because you basically won't encounter it. If you encounter it, you can check this book or the manual to find out.
error_reporting Report error type
error_reporting refers to error reporting. There is also such a parameter in php.ini. this parameter. Determines which error types the PHP engine records, reports, and displays.
1. Set the error_reporting parameter in php.ini. If the error_reporting parameter is set to 0. Errors in the entire PHP engine will not be displayed, output, or recorded. It will not be recorded in the logging that will be discussed in the next chapter.
If we want to display all errors, we can write :
error_reporting = E_ALL
If we want to display all errors but exclude prompts, we can Write this parameter as:
error_reporting = E_ALL & ~ E_NOTICE
Displays all errors, but excludes prompts, compatibility, and future compatibility. It can be written as:
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
2. In some cases, we do not have permission to operate the php.ini file and want to control it What to do with error_reporting?
At the beginning of the running xxxx.php file, we can use the error_reporting() function to achieve the goal.
The demo code is as follows:
<?php //关闭了所有的错误显示 error_reporting(0); //显示所有错误 //error_reporting(E_ALL); //显示所有错误,但不显示提示 //error_reporting(E_ALL & ~ E_NOTICE); ?>
You can try the above code and try to write the wrong code intentionally. Whether the specified error will be displayed in the current file.
[Expand and understand knowledge points]: The @ symbol is a single line that we have learned before and does not display errors. Please do not use the @ symbol or use it sparingly.
Let’s read a non-existent file, such as PHP code to demonstrate the implementation process:
<?php //读取一个不存在的adsaf.txt文件,用@符抑制错误 @$fp = fopen('adsaf.txt','r'); ?>
@ symbol is less efficient, its implementation process in the PHP kernel is:
<?php //关闭错误 error_reporting(0); //读取一个不存在的文件,显示错误 //显示错误 error_reporting(E_ALL & ~ E_NOTICE); ?>