0
0
PHPprogramming~10 mins

Set_error_handler function in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Set_error_handler function
Start Program
Call set_error_handler
Error occurs?
NoContinue normal execution
Yes
Custom error handler called
Handle error (log, display, etc.)
Return control to program
Program ends
The program sets a custom error handler, then when an error happens, it calls that handler to manage the error before continuing.
Execution Sample
PHP
<?php
function myErrorHandler($errno, $errstr) {
  echo "Error: [$errno] $errstr\n";
}
set_error_handler('myErrorHandler');
echo $undefinedVar;
?>
This code sets a custom error handler that prints error details, then triggers an error by using an undefined variable.
Execution Table
StepActionEvaluationResult
1Define function myErrorHandlerFunction createdReady to handle errors
2Call set_error_handler('myErrorHandler')Set custom error handlerErrors now use myErrorHandler
3Execute echo $undefinedVar;Undefined variable error triggeredCall myErrorHandler with error info
4Inside myErrorHandlerPrint error messageOutput: Error: [8] Undefined variable: undefinedVar
5Return from myErrorHandlerError handledProgram continues
6Program endsNo more codeExecution complete
💡 Program ends after handling the undefined variable error with custom handler
Variable Tracker
VariableStartAfter Step 3After Step 5Final
$errnoN/A8 (Notice)8 (Notice)8 (Notice)
$errstrN/A"Undefined variable: undefinedVar""Undefined variable: undefinedVar""Undefined variable: undefinedVar"
$undefinedVarUndefinedUndefined error triggeredUndefined error handledUndefined
Key Moments - 2 Insights
Why does the custom error handler get called when echoing an undefined variable?
Because echoing an undefined variable triggers a notice error, and set_error_handler directs PHP to call the custom handler instead of the default error behavior, as shown in step 3 and 4 of the execution_table.
Does the program stop after the error is handled?
No, after the custom error handler runs (step 4 and 5), the program continues normally until it ends, as shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 4?
AError: [8] Undefined variable: undefinedVar
BFatal error: Undefined variable
CNo output
DWarning: Variable undefinedVar not set
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step does the custom error handler get set?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column to find when set_error_handler is called.
If the error handler function was not set, what would happen at step 3?
AThe program would ignore the error and continue silently
BPHP would use the default error handler and show a notice
CThe program would crash immediately
DThe error would be logged but not displayed
💡 Hint
Recall that without set_error_handler, PHP uses its default error handling for notices.
Concept Snapshot
set_error_handler(functionName)
- Registers a custom function to handle PHP errors
- When an error occurs, PHP calls this function instead of default
- Function receives error number and message
- Allows custom error display or logging
- Program continues after handler unless stopped
Full Transcript
This example shows how PHP's set_error_handler function works. First, a custom function named myErrorHandler is defined to print error details. Then, set_error_handler is called with this function's name, so PHP uses it for errors. When the code tries to echo an undefined variable, PHP triggers a notice error. Instead of default handling, PHP calls myErrorHandler with error info. The handler prints the error message. After handling, the program continues and ends normally. Variables like $errno and $errstr hold error details during handling. This lets programmers control error messages and behavior in their PHP scripts.