0
0
PHPprogramming~20 mins

Set_error_handler function in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Error Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
?>
AError caught: [8] Undefined variable: undefinedVar
BNotice: Undefined variable: undefinedVar in script on line 7
CNo output
DFatal error: Uncaught Error: Undefined variable: undefinedVar
Attempts:
2 left
💡 Hint
The custom error handler replaces the default error message for notices.
🧠 Conceptual
intermediate
1: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?
AAll errors including fatal compile-time errors
BE_NOTICE, E_WARNING, E_USER_ERROR
CE_COMPILE_ERROR, E_CORE_WARNING, E_PARSE
DE_PARSE, E_ERROR, E_CORE_ERROR
Attempts:
2 left
💡 Hint
Some errors occur before the script runs and cannot be caught.
🔧 Debug
advanced
2: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"');
?>
AParse errors cannot be caught by set_error_handler because they occur before runtime.
BThe error handler function name is incorrect.
Ceval() suppresses all errors by default.
DThe error handler must be set after the eval statement.
Attempts:
2 left
💡 Hint
Parse errors happen before the script runs.
📝 Syntax
advanced
2: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?
Aset_error_handler('handler', E_NOTICE);
Bset_error_handler('handler', E_ALL | E_NOTICE);
Cset_error_handler('handler', E_ALL & ~E_NOTICE);
Dset_error_handler('handler', E_ALL ^ E_NOTICE);
Attempts:
2 left
💡 Hint
Use bitwise operators to exclude E_NOTICE.
🚀 Application
expert
3: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;
?>
ANo output
BFirst handler: Undefined variable: undefinedVar
C
First handler: Undefined variable: undefinedVar
Second handler: Undefined variable: undefinedVar
DSecond handler: Undefined variable: undefinedVar
Attempts:
2 left
💡 Hint
The last set_error_handler replaces the previous one.