Recall & Review
beginner
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?✗ 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?
✗ 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?✗ Incorrect
Returning false tells PHP to also run its default error handler.
Can
set_error_handler catch fatal errors like E_ERROR?✗ 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?
✗ 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.