0
0
PHPprogramming~10 mins

Closures as callbacks in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Closures as callbacks
Define Closure
Pass Closure as Callback
Function Calls Callback
Closure Executes
Return Result or Side Effect
End
This flow shows how a closure (a function without a name) is defined, passed as a callback to another function, then called and executed inside that function.
Execution Sample
PHP
<?php
$greet = function($name) {
    return "Hello, $name!";
};

function welcome($callback, $user) {
    echo $callback($user);
}

welcome($greet, "Alice");
This code defines a closure that greets a user, then passes it as a callback to a function that calls it and prints the greeting.
Execution Table
StepActionEvaluationResult
1Define closure $greetClosure createdFunction stored in $greet
2Define function welcomeFunction createdFunction ready to use
3Call welcome($greet, "Alice")welcome called with closure and 'Alice'Execution enters welcome
4Inside welcome: call $callback('Alice')Calls closure with 'Alice'Returns 'Hello, Alice!'
5echo outputPrints 'Hello, Alice!'Output: Hello, Alice!
6welcome endsFunction returns voidProgram ends
💡 welcome function finishes after calling the closure and printing the greeting
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
$greetundefinedClosure functionClosure functionClosure functionClosure function
$callbackundefinedundefinedClosure functionClosure functionClosure function
$userundefinedundefined"Alice""Alice""Alice"
Key Moments - 3 Insights
Why can we call $callback($user) inside the welcome function?
Because $callback holds the closure function passed as an argument, so calling $callback($user) runs that closure with the given user.
Is the closure $greet executed when it is defined?
No, the closure is only created and stored in $greet. It runs later when welcome calls it at step 4.
What does echo inside welcome print?
It prints the string returned by the closure, which is 'Hello, Alice!' as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $callback at step 4?
AUndefined
BString 'Alice'
CClosure function
DInteger 0
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 4 in the execution table.
At which step does the program print the greeting message?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look for the 'echo output' action in the execution table.
If we changed welcome to call $callback with 'Bob' instead of $user, what would change in the variable tracker?
AThe value of $user would change to 'Bob'
BThe value passed to $callback would be 'Bob', but $user stays 'Alice'
CNothing changes anywhere
DThe closure $greet would change
💡 Hint
Check how $user and $callback are tracked separately in the variable tracker.
Concept Snapshot
Closures as callbacks in PHP:
- Define a closure: $fn = function($x) { ... };
- Pass closure as argument to a function
- Call closure inside function: $callback($arg)
- Closure runs with passed argument
- Useful for flexible, reusable code
Full Transcript
This example shows how to use closures as callbacks in PHP. First, a closure is defined and stored in a variable. Then, a function is defined that accepts a callback and a user name. When the function is called with the closure and a user name, it calls the closure with the user name and prints the greeting. The execution table traces each step, showing how variables change and when the closure runs. Key moments clarify common confusions about when the closure executes and how it is called. The quiz tests understanding of variable values and output timing. This helps beginners see how closures can be passed and used as callbacks in PHP.