Suppress error display
In the php.ini configuration file. We can control the error display state of PHP.
There is a special configuration item in the php.ini:
Display_errors
This option sets whether to output error messages to Web pages, or to hide them from the user.
The status of this value is either on off , or you can set the value to 1 either 0 .
- The value of Display_error is set to 0 or off does not display an error on the page.
- Set to 1 or on to display the error message.
Question: What if I do not modify the server php.ini state permissions?
can be used ini_set .
<?php
ini_set(‘display_errors‘ , 0 );
?>
The above code is also equivalent to modifying the value of Display_errors in php.ini. However, only in the 当前php代码中生效 .
Question: What if I want to get the configuration item status of PHP.ini?
You can use Ini_get (parameter entry) to get the value of the parameter.
Demo Example:
<?php
echo ‘服务器中display_errors的状态为‘ . ini_get(‘display_errors‘);
?>
注: After modifying the php.ini file, the server needs to be restarted.
Type of error
PHP Everyone's most common error shows that this book helps you to summarize:
"Mastering the level of error types" We have divided the most common errors into three categories:
| Type of error |
Description |
| E_error |
Error, file is interrupted directly |
| E_warning |
Warning, the problem is more serious. But it will continue to run down |
| E_notice |
Note that some minor problems do not affect the program. Often occurs when a project is not defined |
| E_parse |
Compile-time syntax parsing error. Parsing errors are generated only by the parser. |
| E_all |
All the errors |
| E_strict |
Enable PHP recommendations for code modifications to ensure the best interoperability and forward compatibility of your code. |
| e_deprecated |
When enabled, warns you about code that might not work correctly in a future release. |
In the above several types:
errorMost serious, must be solved. Otherwise the program cannot continue execution down
warningis also important. It has to be solved. If it is clear, intentional can not be handled.
noticeYou can do it without care. But in some companies, project standards are particularly high. It must also be addressed in high-standards-demanding projects. Because, notice will affect the efficiency of PHP execution. Typically occurs when a function is undefined, and so on.
parseError, refers to the wrong grammar, must be resolved
- All errors that represent all types
"Understanding the error type of the level" from these three kinds of extensions came up with some additional error items to understand:
| Type of error |
Error Description |
| e_core_error |
A fatal error occurred during PHP initialization startup. This error is similar to E_error, but is generated by the PHP engine core |
| e_core_warning |
PHP Initialization warning (non-fatal error) occurred during startup. Similar to e_warning, but generated by the core of the PHP engine. |
| e_compile_error |
Fatal compile-time error. Similar to E_error, but generated by the Zend scripting engine. |
| e_compile_warning |
compile-time warning (non-fatal error). Similar to e_warning, but generated by the Zend scripting engine |
| e_user_error |
user custom error |
| e_user_warning |
user-defined warnings |
| e_user_notice |
user-defined prompts |
| e_user_deprecated |
User-produced warning message. Similar to e_deprecated, but is generated by the user himself using the PHP function Trigger_error () in the code. |
| e_recoverable_error |
fatal error that can be captured. It indicates that a potentially very dangerous error has occurred, but has not yet caused the PHP engine to be in an unstable state. |
In the learning process, the above types can be understood. Because basically do not encounter, met the people to check the book or check the manual is clear.
Error_reporting Reporting Error types
Error_reporting refers to the error report. There is also such a parameter in php.ini. This parameter. Determines which error types are logged, reported, and displayed by the PHP engine.
- Error_reporting the parameter in the php.ini. If the error_reporting parameter is set to 0. The entire PHP engine error will not be displayed, output, record.
If we want to show all errors can be written on:
error_reporting = E_all
To display all errors but exclude hints, you can write this parameter as:
error_reporting = E_all & ~ E_notice
Displays all errors, but excludes hints, compatibility, and future compatibility. Can be written as:
error_reporting = E_all & ~e_notice & ~e_strict & ~e_deprecated
- In some cases we do not have permission to operate the php.ini file, but also want to control error_reporting how to do?
At the beginning of the run xxxx.php file, we can use the error_reporting() function gray to reach the target.
The demo code is as follows:
<?php
//关闭了所有的错误显示
error_reporting(0);
//显示所有错误
//error_reporting(E_ALL);
//显示所有错误,但不显示提示
//error_reporting(E_ALL & ~ E_NOTICE);
?>
The above code you can try, deliberately write the wrong code to see. The specified error will not be displayed in the current file.
[extensions, Knowledge points]: The @ character is a single line that we have previously learned does not display errors, please do not use or less with the @ character.
We take a non-existent file, such PHP code to demonstrate the implementation process:
<?php
//读取一个不存在的adsaf.txt文件,用@符抑制错误
@$fp = fopen(‘adsaf.txt‘,‘r‘);
?>
The @ character is inefficient, and its implementation in the PHP kernel is:
<?php
//关闭错误
error_reporting(0);
//读取一个不存在的文件,显示错误
//显示错误
error_reporting(E_ALL & ~ E_NOTICE);
?>
Error logging
The log collection system will silently help you collect errors, warnings, and prompts behind your back. Similar to Java in log4j, and so on, where: PHP errors, warning that these must be received letter.
So the question is-do not let the user see, set the error report level, how to collect errors into the log system?
There are related configuration items that need to be used to php.ini. These two configuration entries are:
| Parameters |
Configuration Items |
Description |
| Log_errors |
On/Off |
Whether to turn on logging |
| Log_errors_max_len |
Integral type, default 1024 |
Single-row error maximum record length |
| Error_log |
Syslog or specified path |
Where is the error log recorded? |
Description
- The log_errors and Log_errors_max_len in the table are very well understood.
- Instead,
error_log specify what path the error exists on. The syslog in the configuration item may be a bit less understandable. Syslog refers to the system to record. The Windows system is inside the log collector of the computer. Linux defaults on:/etc/syslog.conf
[Expand] learn about knowledge points. If the Linux system initiates or modifies log collection. May be stored in a third-party dedicated log collection server.
In addition, PHP specifically prepares a custom error log function for us:
BOOL Error_log (string wrongmistakenlyEliminateInterest[,INT error - I n t Error message type = 0 [, string $ storage target]])
This function can send error messages to the Web server's error log, or to a file.
Common types of error messages:
| error message Type |
Description |
| 0 |
Send to the default Error_log specified location |
| 1 |
Send to the specified message location |
| 3 |
Send to the specified file location |
Example:
<?php
//无法连接到数据库服务器,直接记录到php.ini 中的error_log指定位置
error_log("无法连接到数据库服务器服务器");
//可以发送邮件,但是php.ini必须配置过邮件系统
error_log(‘可以用邮件报告错误,让运维人员半夜起床干活‘,1 ,‘[email protected]‘);
//记录在指定的位置
error_log("我是一个错误哟", 3, "d:/test/my-errors.log");
?>
Attention:
e-mail messages in Error_log may not be familiar to beginners, you can 不用掌握 learn some pieces of knowledge.
possibilities, custom exception function
The beginning of this piece of knowledge is somewhat high. Most people do not have the software engineering, custom error handling experience, it is difficult to imagine the use of the scene. If you want to skip this block of learning, it's perfectly possible, and we support it.
This block knowledge point is not much used in practical applications. If you have a plan to start writing your own frame, or if you have finished the first item of the book.
You can go back and look at the contents of this chapter 11.4.
Two functions commonly used by user-defined errors:
Set_error_handler (Error handling function for callable $ callback)
To set a user-defined error-handling function
Trigger_error (String $error _msg)
Generate a user-level error/warning/notice information
<?php
//定义一个自定义的错误处理函数
function customError($errno, $errstr, $errfile, $errline) {
//输出错误消息
echo "<b>Custom error:</b> [$errno] $errstr<br />";
//输出错误文件和错误行
echo "Error on line $errline in $errfile<br />";
echo "Ending Script";
//中止程序运行
exit;
}
//使用set_error_handler 绑定用户自定义函数
set_error_handler("customError");
$test=2;
//触发自定义错误
if ($test > 1) {
trigger_error("A custom error has been triggered");
}
?>
From for notes (Wiz)
Front-end PHP entry -032-exception handling-application level