PHP Error Handling Functions: Usage, Examples, and Best Practices
This article explains PHP's error handling functions—including error_reporting, set_error_handler, trigger_error, and error_log—describes their purposes, shows how to define custom handlers, and provides a complete code example demonstrating error level configuration, logging, email alerts, and user-friendly messages.
During development, various exceptions such as code errors, database connection failures, or file read errors may occur; PHP provides a set of error handling functions that allow developers to detect and manage these problems effectively.
Purpose of Error Handling Functions
The purpose of error handling functions is to capture and process errors that occur in a program, preventing the script from terminating abruptly or displaying unfriendly messages to users. By using these functions, developers can record error information, send error reports, and present user‑friendly interfaces.
Common Error Handling Functions
error_reporting()
The error_reporting() function sets the level of errors that the current script will report. It accepts a parameter specifying the error level.
Typical error levels include:
E_ALL: report all errors and warnings
E_ERROR: report fatal errors
E_WARNING: report non‑fatal runtime warnings
E_NOTICE: report runtime notices
set_error_handler()
The set_error_handler() function allows you to define a custom error handling function. When an error occurs, PHP will invoke this custom handler.
A typical custom error handler has the following signature:
<code>function custom_error_handler($error_number, $error_message, $error_file, $error_line) {
// handling logic
}</code>Where $error_number is the error level, $error_message is the error description, $error_file is the filename where the error occurred, and $error_line is the line number.
trigger_error()
The trigger_error() function manually triggers an error message. It takes a message parameter and an optional error type.
Common trigger types are:
E_USER_ERROR: fatal user‑generated error
E_USER_WARNING: user‑generated warning
E_USER_NOTICE: user‑generated notice
error_log()
The error_log() function writes an error message to a specified log file. It accepts three parameters: the error message, the log type, and the log file name.
Typical log types include:
0: write the error message to the server's error log
1: send the error message to PHP's system logger
3: write the error message to a specified file
Example of Using Error Handling Functions
The following example demonstrates how to use the error handling functions to capture and process exceptions.
<code>// Set the error reporting level
error_reporting(E_ALL);
// Custom error handling function
function custom_error_handler($error_number, $error_message, $error_file, $error_line) {
// Log error to a file
error_log("Error: [$error_number] $error_message in $error_file on line $error_line", 3, "error.log");
// Send error report to administrator
mail("[email protected]", "Program Error!", "Error details: $error_message");
// Display a friendly message to the user
echo "Sorry, the program encountered an issue. Please try again later!";
}
// Register the custom error handler
set_error_handler("custom_error_handler");
// Trigger a fatal error
trigger_error("This is a fatal error!", E_USER_ERROR);
// Trigger a warning
trigger_error("This is a warning!", E_USER_WARNING);
// Trigger a notice
trigger_error("This is a notice!", E_USER_NOTICE);
</code>In this code, error_reporting() sets the reporting level to E_ALL , then a custom handler custom_error_handler() is defined to log errors, email the administrator, and show a friendly message.
Finally, trigger_error() is used to generate a fatal error, a warning, and a notice, verifying that the custom error handling functions work as intended.
PHP's error handling functions provide powerful tools for managing program exceptions, improving stability and reliability. Mastering their usage enables developers to capture and handle errors effectively, ensuring smooth operation of their projects.
PHP Learning Recommendations
Vue3+Laravel8+Uniapp Beginner to Real‑World Development Tutorial
Vue3+TP6+API Social E‑Commerce System Development Course
Swoole From Beginner to Advanced Course
Workerman+TP6 Real‑Time Chat System – Limited Time Offer
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.