The set_error_handler function lets you control what happens when an error occurs in your PHP program. Instead of showing the default error message, you can create your own way to handle errors.
Set_error_handler function in PHP
set_error_handler(callable $error_handler, int $error_levels = E_ALL): ?callable
The $error_handler is a function you create that will run when an error happens.
The optional $error_levels lets you choose which errors to catch (default is all errors).
<?php function myErrorHandler($errno, $errstr, $errfile, $errline) { echo "Error [$errno]: $errstr in $errfile on line $errline\n"; } set_error_handler('myErrorHandler');
<?php set_error_handler(function($errno, $errstr) { echo "Custom error: $errstr\n"; });
<?php
set_error_handler('myErrorHandler', E_WARNING | E_NOTICE);This program sets a custom error handler that prints a friendly message when an error happens. Then it causes two errors: dividing by zero and using an undefined variable. Instead of default errors, the custom messages show.
<?php function customErrorHandler($errno, $errstr, $errfile, $errline) { echo "Oops! Error number $errno happened: $errstr on line $errline in file $errfile\n"; } set_error_handler('customErrorHandler'); // This will cause a warning (division by zero) echo 10 / 0; // This will cause a notice (undefined variable) echo $undefinedVar;
The error handler function must accept at least four parameters: error number, error message, file name, and line number.
After setting a custom error handler, PHP will use it for errors matching the specified levels.
Remember to restore the original error handler if needed using restore_error_handler().
set_error_handler lets you catch and handle errors your way.
You create a function that runs when an error happens.
This helps make your program friendlier and easier to debug.
Practice
set_error_handler function in PHP?Solution
Step 1: Understand what
This function lets you tell PHP to use your own function to handle errors instead of the default error messages.set_error_handlerdoesStep 2: Compare options with this understanding
Only defining a custom function to handle errors when they occur correctly describes this behavior. Automatically fixing errors, stopping all errors from appearing, and logging errors to a file without any custom handling are all incorrect.Final Answer:
To define a custom function to handle errors when they occur -> Option AQuick Check:
Custom error handling = Define function with set_error_handler [OK]
- Thinking it stops errors completely
- Assuming it fixes errors automatically
- Confusing it with error logging functions
myErrorHandler?Solution
Step 1: Recall the syntax for passing function names in PHP
When passing a function name as a callback, it must be a string in quotes.Step 2: Check each option
set_error_handler('myErrorHandler');correctly passes the function name as a string.set_error_handler(myErrorHandler);misses quotes,set_error_handler(&myErrorHandler);uses an invalid reference operator, andset_error_handler($myErrorHandler);uses a variable which is incorrect here.Final Answer:
set_error_handler('myErrorHandler'); -> Option CQuick Check:
Function name as string = 'myErrorHandler' [OK]
- Omitting quotes around function name
- Using variable instead of string
- Using reference operator & incorrectly
function customError($errno, $errstr) {
echo "Error: [$errno] $errstr";
}
set_error_handler('customError');
trigger_error('This is a test error', E_USER_WARNING);Solution
Step 1: Understand the custom error handler and trigger_error
The customError function prints the error number and message. The trigger_error calls this handler with E_USER_WARNING which has error number 512.Step 2: Predict the output
The output will be the echo from customError showing the error number and message exactly as coded.Final Answer:
Error: [512] This is a test error -> Option BQuick Check:
Custom handler prints error number 512 and message [OK]
- Expecting default PHP warning format
- Confusing error numbers
- Thinking no output appears
set_error_handler:function handler($errno, $errstr) {
echo "Error: $errstr";
}
set_error_handler(handler);
trigger_error('Oops!');Solution
Step 1: Check the set_error_handler syntax
The function name passed to set_error_handler must be a string, so it needs quotes.Step 2: Verify trigger_error usage
trigger_error can be called with one argument; the second is optional. The handler function does not need to return a value. Using trigger_error with set_error_handler is valid.Final Answer:
Missing quotes around handler in set_error_handler -> Option DQuick Check:
Function name must be string in quotes [OK]
- Omitting quotes around function name
- Thinking trigger_error needs two arguments
- Expecting handler to return a value
set_error_handler is best?Solution
Step 1: Understand the goal
You want to capture warnings and notices and save them to a file instead of showing them on screen.Step 2: Evaluate options
Create a function that writes error details to a log file and set it with set_error_handler correctly describes creating a custom function that writes error info to a log file and setting it as the error handler. Use set_error_handler with an empty function to ignore errors ignores errors, which is not logging. Call trigger_error inside set_error_handler to log errors misuses trigger_error inside the handler, causing recursion. Disable error reporting and rely on PHP default logging disables reporting but does not customize logging.Final Answer:
Create a function that writes error details to a log file and set it with set_error_handler -> Option AQuick Check:
Custom handler writes logs = best practice [OK]
- Ignoring errors instead of logging
- Calling trigger_error inside handler causing loops
- Disabling errors without custom logging
Ready to prove it?
Pass the test and this pattern moves unknown
