php study notes--error
Different error handling methods:
Simple die() statement to customize error function and error trigger error reporting
Basic error handling: use the die() function
if(!file_exists("welcome.txt")){
die("FIle not found");
}else{
$file=fopen("welcome.txt","r");
}
or
$file=fopen("webdictionary.txt","r") or die("Unable to open file!");
die(status): If status is a string, this function will output the string before ejecting.
If status is a certificate, this value will be used as the exit status. The exit status has a value between 0 and 254. Exit status 255 is reserved by PHP and will not be used. Status 0 is used to terminate the program successfully.
Create a custom error handler:
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(error_level,error_message,error_file,error_line,error_context)//Error reporting level, error message, file name and line number to send the error, specify an array (containing each variable used when the error occurs and their values) )
value |
Constant |
Description |
2 |
E_WARNING |
Nonfatal run-time error. Do not pause script execution. |
8 |
E_NOTICE |
值 |
常量 |
描述 |
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 |
所有错误和警告,除级别 E_STRICT 以外。
(在 PHP 6.0,E_STRICT 是 E_ALL 的一部分)
|
Run-time notification. Script discovery errors can occur, but they can also occur while the script is running normally.
|
256 |
E_USER_ERROR |
Fatal user-generated error. This is similar to E_ERROR set by the programmer using the PHP function trigger_error().
|
512 |
E_USER_WARNING |
Non-fatal user-generated warning. This is similar to the E_WARNING set by the programmer using the PHP function trigger_error().
|
1024 |
E_USER_NOTICE |
User-generated notifications. This is similar to E_NOTICE set by the programmer using the PHP function trigger_error().
|
4096 |
E_RECOVERABLE_ERROR |
Catchable fatal error. Like E_ERROR, but can be caught by a user-defined handler. (see set_error_handler())
|
8191 |
E_ALL |
All errors and warnings except level E_STRICT.
(In PHP 6.0, E_STRICT is part of E_ALL)
|
function customError($errno,$errstr){
echo "
Error:[$errno] $errstr
";
echo "Ending Script";
die();
}
When the above error is triggered, it gets the error level and error message. It then prints the error level and message, and terminates the script. Now that we have created an error handling function, we need to determine when to fire it.
Set Error Handler:
PHP's default error handler is the mole's error handler. Error handlers can be modified to apply only to certain errors, so that the script can handle different errors in different ways.
In this case, we are going to use our custom error handler for all errors.
set_error_handle(“customError”);
When handling all errors, set_error_handler() requires only one parameter, and a second parameter can be added to specify the error level.
Trigger error:
Triggers an error when the user's input is invalid. Completed by trigger_error().
$test=2;
if($test>1){
trigger_error("Value must be 1 or below");//An error is triggered when it is greater than 1
}
Possible error types:
E_USER_ERROR:
E_USER_WARNING:
E_USER_NOTICE:
http://www.bkjia.com/PHPjc/851379.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/851379.html
TechArticle