Bird
0
0

You want to log all warnings and notices to a file instead of displaying them. Which approach using set_error_handler is best?

hard📝 Application Q15 of 15
PHP - Error and Exception Handling
You want to log all warnings and notices to a file instead of displaying them. Which approach using set_error_handler is best?
ACreate a function that writes error details to a log file and set it with set_error_handler
BUse set_error_handler with an empty function to ignore errors
CCall trigger_error inside set_error_handler to log errors
DDisable error reporting and rely on PHP default logging
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    You want to capture warnings and notices and save them to a file instead of showing them on screen.
  2. 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.
  3. Final Answer:

    Create a function that writes error details to a log file and set it with set_error_handler -> Option A
  4. Quick Check:

    Custom handler writes logs = best practice [OK]
Quick Trick: Custom handler writes logs to file for error control [OK]
Common Mistakes:
  • Ignoring errors instead of logging
  • Calling trigger_error inside handler causing loops
  • Disabling errors without custom logging

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes