What is the purpose of the set_error_handler function in PHP?
The set_error_handler function lets you define a custom function to handle errors instead of PHP's default error handler. This helps you control how errors are processed and displayed.
Click to reveal answer
beginner
How do you define a custom error handler function for set_error_handler?
You create a function that accepts parameters like error level, error message, file, and line number. Then you pass its name to set_error_handler to use it for handling errors.
Click to reveal answer
intermediate
What parameters does the custom error handler function receive?
It receives these parameters:
$errno: The error level
$errstr: The error message
$errfile: The file where the error occurred
$errline: The line number of the error
$errcontext (deprecated as of PHP 7.2.0): An array of variables in scope
Click to reveal answer
intermediate
What happens if the custom error handler returns false?
If the custom error handler returns false, PHP will execute the default error handler as well. Returning true or nothing prevents the default handler from running.
Click to reveal answer
advanced
Can set_error_handler handle fatal errors like E_ERROR?
No, set_error_handler cannot handle fatal errors like E_ERROR or parse errors. It only handles non-fatal errors like warnings and notices. For fatal errors, you can use register_shutdown_function with error_get_last().
Click to reveal answer
What does set_error_handler do in PHP?
ALogs errors to a file automatically
BSets a custom function to handle errors
CStops all errors from occurring
DFixes syntax errors in code
✗ Incorrect
set_error_handler lets you specify your own function to manage errors instead of PHP's default way.
Which parameter is NOT passed to a custom error handler function?
A$errtrace (stack trace)
B$errstr (error message)
C$errcontext (variables in scope)
D$errno (error level)
✗ Incorrect
The $errtrace parameter is not passed to the error handler. Stack traces are not given automatically.
What happens if your custom error handler returns false?
APHP runs the default error handler too
BPHP logs the error silently
CPHP stops the script immediately
DPHP ignores the error
✗ Incorrect
Returning false tells PHP to also run its default error handler.
Can set_error_handler catch fatal errors like E_ERROR?
AYes, always
BOnly if error_reporting is set to E_ALL
CNo, it cannot
DOnly in PHP 8+
✗ Incorrect
Fatal errors are not handled by set_error_handler. Use shutdown functions instead.
Which function can you use to restore the previous error handler?
Aremove_error_handler()
Breset_error_handler()
Cclear_error_handler()
Drestore_error_handler()
✗ Incorrect
restore_error_handler() resets the error handler to what it was before.
Explain how to create and use a custom error handler with set_error_handler in PHP.
Think about the function signature and how PHP calls it on errors.
You got /4 concepts.
What types of errors can set_error_handler handle and which ones it cannot?
Consider the difference between recoverable and fatal errors.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the set_error_handler function in PHP?
easy
A. To define a custom function to handle errors when they occur
B. To stop all errors from appearing
C. To automatically fix errors in the code
D. To log errors to a file without any custom handling
Solution
Step 1: Understand what set_error_handler does
This function lets you tell PHP to use your own function to handle errors instead of the default error messages.
Step 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 A
Quick Check:
Custom error handling = Define function with set_error_handler [OK]
Hint: Remember: set_error_handler sets your own error function [OK]
Common Mistakes:
Thinking it stops errors completely
Assuming it fixes errors automatically
Confusing it with error logging functions
2. Which of the following is the correct syntax to set a custom error handler function named myErrorHandler?
easy
A. set_error_handler(&myErrorHandler);
B. set_error_handler(myErrorHandler);
C. set_error_handler('myErrorHandler');
D. set_error_handler($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, and set_error_handler($myErrorHandler); uses a variable which is incorrect here.
Final Answer:
set_error_handler('myErrorHandler'); -> Option C
Quick Check:
Function name as string = 'myErrorHandler' [OK]
Hint: Function names as strings in callbacks [OK]
Common Mistakes:
Omitting quotes around function name
Using variable instead of string
Using reference operator & incorrectly
3. What will be the output of the following PHP code?
function customError($errno, $errstr) {
echo "Error: [$errno] $errstr";
}
set_error_handler('customError');
trigger_error('This is a test error', E_USER_WARNING);
medium
A. Warning: This is a test error
B. Error: [512] This is a test error
C. Fatal error: Uncaught Error
D. No output
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 B
Quick Check:
Custom handler prints error number 512 and message [OK]
Hint: E_USER_WARNING error number is 512 [OK]
Common Mistakes:
Expecting default PHP warning format
Confusing error numbers
Thinking no output appears
4. Identify the error in this PHP code snippet using set_error_handler:
function handler($errno, $errstr) {
echo "Error: $errstr";
}
set_error_handler(handler);
trigger_error('Oops!');
medium
A. trigger_error requires two arguments
B. trigger_error cannot be used with set_error_handler
C. handler function must return a value
D. Missing quotes around handler in set_error_handler
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 D
Quick Check:
Function name must be string in quotes [OK]
Hint: Function names need quotes in set_error_handler [OK]
Common Mistakes:
Omitting quotes around function name
Thinking trigger_error needs two arguments
Expecting handler to return a value
5. You want to log all warnings and notices to a file instead of displaying them. Which approach using set_error_handler is best?
hard
A. Create a function that writes error details to a log file and set it with set_error_handler
B. Use set_error_handler with an empty function to ignore errors
C. Call trigger_error inside set_error_handler to log errors
D. Disable error reporting and rely on PHP default logging
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 A
Quick Check:
Custom handler writes logs = best practice [OK]
Hint: Custom handler writes logs to file for error control [OK]