Challenge - 5 Problems
PHP Error Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using set_error_handler?
Consider the following PHP code that sets a custom error handler. What will it output when run?
PHP
<?php function customError($errno, $errstr) { echo "Error caught: [$errno] $errstr\n"; } set_error_handler('customError'); // Trigger an error echo $undefinedVar; ?>
Attempts:
2 left
💡 Hint
The custom error handler replaces the default error message for notices.
✗ Incorrect
The set_error_handler function sets a user-defined function to handle errors. When the undefined variable is accessed, a notice is triggered. Instead of the default PHP notice, the customError function runs and outputs the formatted message.
🧠 Conceptual
intermediate1:30remaining
Which error types can be handled by set_error_handler?
In PHP, which of the following error types can be caught and handled by a function set with set_error_handler?
Attempts:
2 left
💡 Hint
Some errors occur before the script runs and cannot be caught.
✗ Incorrect
set_error_handler can handle runtime errors like notices, warnings, and user-generated errors. It cannot handle parse errors or fatal errors that stop script execution before the handler is set.
🔧 Debug
advanced2:30remaining
Why does this custom error handler not catch the error?
Look at this PHP code. The custom error handler is set, but the error is not caught by it. Why?
PHP
<?php function myErrorHandler($errno, $errstr) { echo "Caught error: $errstr\n"; } // Intentionally trigger a parse error set_error_handler('myErrorHandler'); eval('echo "Hello"'); ?>
Attempts:
2 left
💡 Hint
Parse errors happen before the script runs.
✗ Incorrect
Parse errors happen during the compilation phase before the script runs, so set_error_handler cannot catch them. Only runtime errors can be caught.
📝 Syntax
advanced2:00remaining
Which option correctly sets a custom error handler that ignores E_NOTICE errors?
You want to set a custom error handler in PHP that handles all errors except notices (E_NOTICE). Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
Use bitwise operators to exclude E_NOTICE.
✗ Incorrect
E_ALL & ~E_NOTICE means all errors except notices. The bitwise AND with the negation of E_NOTICE excludes notices from the handled errors.
🚀 Application
expert3:00remaining
What is the output of this PHP code with nested error handlers?
Analyze this PHP code that sets two error handlers one after another. What will be the output when an undefined variable is accessed?
PHP
<?php function firstHandler($errno, $errstr) { echo "First handler: $errstr\n"; return false; // Pass to next handler } function secondHandler($errno, $errstr) { echo "Second handler: $errstr\n"; } set_error_handler('firstHandler'); set_error_handler('secondHandler'); echo $undefinedVar; ?>
Attempts:
2 left
💡 Hint
The last set_error_handler replaces the previous one.
✗ Incorrect
Only the last error handler set is active. The firstHandler is replaced by secondHandler. So only secondHandler runs and outputs the message.