Home > Article > Backend Development > php ignore bad code
PHP is a very popular Web programming language. Its power is not only reflected in its flexibility and ease of use, but also in its error handling mechanism. When an error occurs in the PHP code, the PHP script will throw an error message by default and stop running, but sometimes we need to ignore these errors and let the script continue to run. So, how do you ignore errors in PHP? This article will introduce it to you in detail.
1. Error levels
In PHP, errors are divided into different levels, such as fatal errors, serious errors, warnings, etc. Different error levels will be handled differently at runtime. Fatal errors will directly stop the script from running, while serious errors and warnings will throw error messages but will not stop the script from running. Different error constants are defined in PHP, which correspond to different error levels. The following are common PHP error constants and their corresponding error levels:
2. Error handling
PHP provides some functions to handle errors , such as error log function error_log(), throw exception function throw(), display error information function error_reporting(), etc. Of course, we can also customize error handling functions. The following is a brief introduction to the usage of these functions.
(1) error_log()
The error_log() function is used to record error information to the server error log file, such as Apache's error_log file. This function has three parameters:
bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )
$message is the error message, $message_type is the error level (the default is 0, which is ignored), $destination is the error log file path, and $extra_headers can set additional header information for the email.
(2) throw()
Thethrow() function is used to throw an exception, that is, to directly stop the program from running and output an error message. This function has one parameter, which is an instance of the exception object.
(3) error_reporting()
The error_reporting() function is used to set the error reporting level. It has one parameter, which is an error constant or the bitwise OR operation result of several error constants. .
The following is a simple example showing how to use the error_reporting() function to ignore E_NOTICE level errors:
error_reporting(E_ALL & ~E_NOTICE);
By performing a bitwise AND operation on E_ALL with ~E_NOTICE, you can get the result except E_NOTICE All error levels except After this setting, the PHP script will ignore E_NOTICE level errors.
(4) Custom error handling function
PHP allows us to customize the error handling function, so that we can customize the error log file path, or the error output method, etc. We need to use the set_error_handler function to set the error handling function. This function has one parameter, which is the name of the custom processing function.
The following is a simple example:
function custom_error_handler($errno, $errstr, $errfile, $errline) { error_log($errstr . "in" . $errfile . " on line " . $errline); return true; } set_error_handler("custom_error_handler");
This custom error handling function writes error information to the server's error log file and returns a true value, indicating that the error has been handled .
3. Ignore errors
Now let’s look at how to ignore errors in PHP. The method is very simple, just use the @ symbol to ignore errors that you don't want to handle. For example:
$count = @file_get_contents("file.txt");
This statement will read the contents of the file "file.txt" and assign it to the variable $count, but if an error occurs when reading the file, such as the file does not exist or does not have permission to read, etc. , then adding the @ symbol can ignore the error and allow the script to continue executing.
It should be noted that using the @ symbol to ignore errors will make the code less safe, because it may lead to some hard-to-find errors, such as uninitialized variables or incorrect function parameters, etc. Therefore, you should only use the @ symbol if you are sure that those errors can be ignored.
4. Summary
In PHP, the method to ignore errors is very simple, just use the @ symbol. But using the @ symbol may cause some hard-to-find errors, so we should try to avoid using it. If you want to ignore errors, you should use the set_error_handler() function to customize the error handling function, or use the error_reporting() function to set the error reporting level. This allows us to handle errors in PHP programs more flexibly and safely.
The above is the detailed content of php ignore bad code. For more information, please follow other related articles on the PHP Chinese website!