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.
#Error handling is an important part when creating scripts and web applications. 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 handling methods for you:
Simple "die()" statement
Custom errors and error triggers
Error reporting
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 You will get an error similar to this:
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:
No such file or directory in /www/php/test/test. php on line 2
In order to avoid users getting error messages similar to the above, we check whether the file exists before accessing it:
<?php if(!file_exists("welcome.txt")) { die("文件不存在"); } else { $file=fopen("welcome.txt","r"); } ?>
Now if the file does not exist, it will be output directly File does not exist
Create custom error handler
Creating a custom 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):
Syntax
##error_function(error_level,error_message,error_file,error_line,error_context)
Error reporting levels
These error reporting levels are different types of errors handled by user-defined error handlers: 2 E_WARNING Non-fatal run-time error. Do not pause script execution. 8 E_NOTICE run-time notification. Occurs when the script finds a possible error, but can also occur when 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. (In PHP 5.4, E_STRICT becomes part of E_ALL)
Create a function that handles errors
<?php function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr<br>"; echo "脚本结束"; die(); } ?>
The above code is a simple Error handling function. When it 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 trigger the function
Set up the error handler
PHP's default error handler is the built-in error handler. We are going to transform the above function into the default error handler when the script is running.
You can modify the error handler so that it only applies to certain errors, so that the script can handle different errors in different ways. However, in this case we are going to use our custom error handler for all errors
set_error_handler("customError");