PHP set_error_handler() registers a callable to handle PHP errors at chosen severity levels. The examples below show how the call behaves, where it is useful, and which limitations matter.
What Is set_error_handler() in PHP?
Registers a callable to handle PHP errors at chosen severity levels.
Its call structure is:
set_error_handler($callback, [$error_levels])
The callable receives severity and message (and optional context arguments supported by PHP); the optional level mask determines handled error severities.
Example 1: Basic Usage
This example introduces set_error_handler with a direct call and a small, readable input.
<?php
set_error_handler(function (int $severity, string $message): bool { echo "Handled: " . $message; return true; });
trigger_error("Invalid option", E_USER_WARNING);
Registers a callable to handle PHP errors at chosen severity levels. A custom handler does not catch fatal engine errors, and should return false when PHP should also process the error.
Example 2: Another Behavior or Input
Compare this input with the previous example: changing the data or option can affect how set_error_handler behaves.
<?php
set_error_handler(function (int $severity, string $message): bool { return $severity !== E_USER_WARNING; }, E_USER_WARNING);
restore_error_handler();
The changed input illustrates why output may differ with the chosen values or environment. A custom handler does not catch fatal engine errors, and should return false when PHP should also process the error.
Practical Example: Using the Feature
The following example incorporates set_error_handler into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.
<?php
set_error_handler(function (int $severity, string $message): bool { error_log("Application warning: " . $message); return true; }, E_USER_WARNING);
// Restore the previous handler when this section is finished.
restore_error_handler();
In a real application, check each operation result, validate values before use and clean up acquired resources. A custom handler does not catch fatal engine errors, and should return false when PHP should also process the error.
Important Behavior and Edge Cases
- Purpose: Registers a callable to handle PHP errors at chosen severity levels.
- Key limitation: A custom handler does not catch fatal engine errors, and should return false when PHP should also process the error.
- Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.
Comparison With Related Functions
| Function or Approach | Best Fit |
|---|---|
set_error_handler() | Registers a callable to handle PHP errors at chosen severity levels. |
restore_error_handler() | Restores the previously installed error handler; set_error_handler() installs a custom handler. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| A custom handler does not catch fatal engine errors, and should return false when PHP should also process the error. | The correct approach depends on this behavior: A custom handler does not catch fatal engine errors, and should return false when PHP should also process the error. |
| Ignoring errors or cleanup after the operation. | Validate inputs and check each result; close files, restore settings or release resources when the operation requires it. |
Key Takeaways
- Registers a callable to handle PHP errors at chosen severity levels.
- A custom handler does not catch fatal engine errors, and should return false when PHP should also process the error.
- Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.
set_error_handler is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.