You want to capture all PHP warnings and notices and write them to a log file using a custom error handler. Which of the following implementations correctly uses set_error_handler for this purpose?
hard📝 Application Q8 of 15
PHP - Error and Exception Handling
You want to capture all PHP warnings and notices and write them to a log file using a custom error handler. Which of the following implementations correctly uses set_error_handler for this purpose?
ADefine a handler function that writes errors to a file and call <code>set_error_handler('handler')</code> with appropriate error levels
BUse <code>set_error_handler</code> without defining a handler function, PHP will log automatically
CCall <code>set_error_handler</code> with an anonymous function that echoes errors to the screen
DUse <code>error_reporting(0)</code> and <code>set_error_handler</code> to suppress errors
Step-by-Step Solution
Solution:
Step 1: Understand logging requirements
To log warnings and notices, a custom handler must be defined that writes error details to a file.
Step 2: Analyze options
Define a handler function that writes errors to a file and call set_error_handler('handler') with appropriate error levels correctly describes defining a handler and setting it with set_error_handler. Use set_error_handler without defining a handler function, PHP will log automatically is incorrect as PHP does not log automatically without a handler. Call set_error_handler with an anonymous function that echoes errors to the screen echoes errors instead of logging. Use error_reporting(0) and set_error_handler to suppress errors suppresses errors, which is opposite of logging.
Final Answer:
Define a handler function that writes errors to a file and call set_error_handler('handler') with appropriate error levels -> Option A
Quick Check:
Custom handler must write to file [OK]
Quick Trick:Custom handler must write to file [OK]
Common Mistakes:
Assuming PHP logs errors automatically
Echoing errors instead of logging
Suppressing errors instead of handling
Master "Error and Exception Handling" in PHP
9 interactive learning modes - each teaches the same concept differently