PHP Error Handling
In PHP, the default error handling is very simple. An error message is sent to the browser, the message with the file name, line number and a description of the error message.
PHP Error Handling
When creating scripts and Web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional, but also opens the door to security risks.
This tutorial introduces some of the most important in PHP error detection method.
We will explain your different error handling methods:
- Simple "die ()" statement
- Custom errors and error triggers
- error report
Basic Error Handling: Using the die () function
The first example shows a simple script that opens a text file:
<?php $file=fopen("welcome.txt","r"); ?>
If the file does not exist, you'll get an error like this:
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream: No such file or directory in /www/w3big/test/test.php on line 2
To prevent users from getting an error message similar to the above, we examined the file before accessing the file exists:
<?php if(!file_exists("welcome.txt")) { die("文件不存在"); } else { $file=fopen("welcome.txt","r"); } ?>
Now, if the file does not exist, you will get this error message similar to:
文件不存在
Compared to the previous code, the code above is more efficient, since it uses a simple error handling mechanism to terminate the script after the error.
However, simply terminate the script is not always the right way. Let us look at alternative PHP functions for handling errors.
Creating a Custom Error Handler
Create a custom error handler is quite simple. We simply create a special function that can be called when an error occurs in PHP.
This function must be capable of handling at least two parameters (error level and error message), but can accept up to five parameters (optionally: file, line-number and error context):
grammar
error_function(error_level,error_message, error_file,error_line,error_context)
parameter | description |
---|---|
error_level | Required. Error is defined as a user-defined error reporting level. It must be a number. See the following table: Error reporting level. |
error_message | Required. User-defined error error message stated. |
error_file | Optional. Filename specified error occurred. |
error_line | Optional. Predetermined line number the error occurred. |
error_context | Optional. Specifies an array containing every variable when an error occurs in use and their values. |
Error reporting level
These error report levels are the different types of errors Error Handler custom processing:
值 | 常量 | 描述 |
---|---|---|
2 | E_WARNING | 非致命的 run-time 错误。不暂停脚本执行。 |
8 | E_NOTICE | run-time 通知。在脚本发现可能有错误时发生,但也可能在脚本正常运行时发生。 |
256 | E_USER_ERROR | 致命的用户生成的错误。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_ERROR。 |
512 | E_USER_WARNING | 非致命的用户生成的警告。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_WARNING。 |
1024 | E_USER_NOTICE | 用户生成的通知。这类似于程序员使用 PHP 函数 trigger_error() 设置的 E_NOTICE。 |
4096 | E_RECOVERABLE_ERROR | 可捕获的致命错误。类似 E_ERROR,但可被用户定义的处理程序捕获。(参见 set_error_handler()) |
8191 | E_ALL | 所有错误和警告。(在 PHP 5.4 中,E_STRICT 成为 E_ALL 的一部分) |
Now, let's create a function to handle errors:
function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr<br>"; echo "脚本结束"; die(); }
The code above is a simple error handling function. When it is triggered, it will get the error level and error messages. It then outputs the error level and message and terminates the script.
Now, we have created an error handling function, we need to determine when to trigger the function.
Setting error handler
PHP's default error handler is a built-in error handler. We are going to transform the function above the default error handler duration of the script.
You can modify the error handler so that it applied only to some error, so the script can be different ways to handle different errors. However, in this case, we intend to use for all errors Error Handler our custom:
set_error_handler("customError");
Since we want our custom function to handle all errors, set_error_handler () only needed one parameter, you can add a second argument to specify an error level.
Examples
By trying to output variable that does not exist, to test the error handler:
<?php // 错误处理函数 function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr"; } // 设置错误处理函数 set_error_handler("customError"); // 触发错误 echo($test); ?>
The output of the code above is as follows:
Error: [8] Undefined variable: test
Trigger Error
In the script the user input location data when the user input is not triggered when an error is useful. In PHP, this is done by the trigger_error () function to complete.
Examples
In this example, if the "test" variable is greater than "1", an error occurs:
<?php $test=2; if ($test>1) { trigger_error("变量值必须小于等于 1"); } ?>
The output of the code above is as follows:
Notice: 变量值必须小于等于 1 in /www/test/w3big.php on line 5
You can trigger an error in the script anywhere, by adding a second parameter, you can specify the error level is triggered.
Possible types of errors:
- E_USER_ERROR - Fatal user-generated run-time error. Unrecoverable error. Script execution is interrupted.
- E_USER_WARNING - Non-fatal user-generated run-time warning. Script execution is not interrupted.
- E_USER_NOTICE - default. User-generated run-time notice. When the script found an error may occur, but may also occur in normal operation script.
Examples
In this example, if the "test" variable is greater than "1", E_USER_WARNING error occurs. If this happens E_USER_WARNING, we will use our custom error handler and end the script:
<?php // 错误处理函数 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); } ?>
The output of the code above is as follows:
Error: [512] 变量值必须小于等于 1 脚本结束
Now that we've learned how to create your own error, and how to trigger them, let's look at the error log.
Error Log
In case of default, according to the error_log configuration is set in the php.ini, PHP sends an error recording system to record or file server. By using the error_log () function you can send error logs to a specified file or a remote destination.
It sends an error message to yourself by e-mail is a good way to get notified of specific errors.
Error message is sent by E-Mail
In the following example, if a specific error occurs, we will send an e-mail with an error message and end the script:
<?php // 错误处理函数 function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr<br>"; echo "已通知网站管理员"; error_log("Error: [$errno] $errstr",1, "someone@example.com","From: webmaster@example.com"); } // 设置错误处理函数 set_error_handler("customError",E_USER_WARNING); // 触发错误 $test=2; if ($test>1) { trigger_error("变量值必须小于等于 1",E_USER_WARNING); } ?>
The output of the code above is as follows:
Error: [512] 变量值必须小于等于 1 已通知网站管理员
Messages received from the code above is as follows:
Error: [512] 变量值必须小于等于 1
This method is not suitable for all errors. Conventional error should be by using the default PHP logging system for recording on the server.