PHP error handling
In PHP, the default error handling is simple. An error message is sent to the browser with the file name, line number, and a message describing the error.
PHP Error Handling
When creating scripts and web applications Error treatment is an important part. If your code lacks error detection coding, the program will look unprofessional and open the door to security risks.
This tutorial introduces some of the most important error detection methods in PHP.
# We will explain different error treatment methods for you:
· Simple "DIE ()" statement
## · Customized definition Error and error trigger
· Error report# This Basic error treatment: Use DIE () function
## The first example shows a simple script that opens the text file: r
## If the file does not exist, you will get the following errors like this:
##11: FOPEN (error.txt) [function.fopen]: failed to Open Stream:No Such File or Directory in /www/php/test/test.php on LINE 2This is to avoid users from getting similar errors, we are visiting File previously detected if the file exists:
<?php $open=fopen('error.txt','r'); echo $open; ?>Now, if the file does not exist, you will get an error message like this:
File does not exist
Compared to the previous code, the above code is more efficient because it uses a simple error handling mechanism to terminate the script after an error. However, simply terminating the script is not always the appropriate approach. Let's examine alternative PHP functions for handling errors.
Create a custom error handler
Create a custom The error handler is very simple. We simply created a dedicated function that can be called when an error occurs in PHP.
The function must be able to handle at least two parameters (error level and error message), but can accept up to five parameters (optional: file, line-number and error context):
error_function
Parameter Description | |
## error_level
| Required. Specifies error messages for user-defined errors. |
Optional. Specifies the file name where the error occurred.
Optional. Specifies the line number where the error occurred. | |||||||||||||||||||
Optional. Specifies an array containing each variable in use when the error occurred and their values. |
Value | Constant | Description | |||||||||||||||||
E_WARNING | Non-fatal run-time error. Do not pause script execution. | ||||||||||||||||||
8 |
| ||||||||||||||||||
256 |
| ||||||||||||||||||
512 | ##E_USER_WARNING |
Example <?php header("Content-type:text/html;charset=utf-8"); // 错误处理函数 function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr<br>"; echo "脚本结束"; die(); } // 设置错误处理函数 set_error_handler("customError",E_USER_WARNING); // 触发错误 $test=2; if ($test>1) { trigger_error("变量值必须小于等于 1",E_USER_WARNING); } ?> Example ##In the example below, if a specific error occurs, we will send an email with the error message and end the script: <?php //无法连接到数据库服务器,直接记录到php.ini 中的error_log指定位置 error_log("无法连接到数据库服务器服务器"); //可以发送邮件,但是php.ini必须配置过邮件系统 error_log('可以用邮件报告错误,让运维人员半夜起床干活',1 ,'liwenkai@phpxy.com'); //记录在指定的位置 error_log("我是一个错误哟", 3, "d:/test/my-errors.log"); ?> Program running results :
The email received from the above code looks like this: This method is not suitable for all errors. General errors should be logged on the server using the default PHP logging system. Note: Sending emails in error_log may be unfamiliar to beginners, so you don’t need to master some knowledge. error_reporting Report error type error_reporting refers to the error Report. 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 show all errors we can write: Want to show all errors but exclude Tip, you can write this parameter as: ##error_reporting = E_ALL & ~ E_NOTICE##error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED #2. In some cases, what should we do if we do not have permission to operate the php.ini file and want to control error_reporting? At the beginning of the running xxxx.php file, we can use the error_reporting() function to achieve the goal. <?php //error handler function function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr<br />"; echo "Webmaster has been notified"; error_log("Error: [$errno] $errstr",1, "someone@example.com","From: webmaster@example.com"); } //set error handler set_error_handler("customError",E_USER_WARNING); //trigger error $test=2; if ($test>1) { trigger_error("Value must be 1 or below",E_USER_WARNING); } ?> 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]: @ symbol is a single line that we have learned before that does not display errors , please do not use or use less @ symbol. Example Let's read a non -existent file.
Continuing Learning
Students who have watched this course are also learning |