0
0
PHPprogramming~5 mins

Set_error_handler function in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ALogs errors to a file automatically
BSets a custom function to handle errors
CStops all errors from occurring
DFixes syntax errors in code
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)
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
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+
Which function can you use to restore the previous error handler?
Aremove_error_handler()
Breset_error_handler()
Cclear_error_handler()
Drestore_error_handler()
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.