0
0
PHPprogramming~10 mins

Closures and variable binding with use in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Closures and variable binding with use
Define outer function
Create closure with 'use'
Closure captures variables by value
Call closure
Closure uses captured variables
Return or output result
This flow shows how a closure captures variables from its surrounding scope using 'use', then uses them when called.
Execution Sample
PHP
<?php
$greeting = 'Hello';
$greeter = function($name) use ($greeting) {
    return "$greeting, $name!";
};
echo $greeter('Alice');
?>
This code creates a closure that captures $greeting by value and uses it to greet a given name.
Execution Table
StepActionVariable StateClosure BehaviorOutput
1Set $greeting = 'Hello'$greeting='Hello'N/AN/A
2Define closure $greeter capturing $greeting$greeting='Hello'Closure captures $greeting by value as 'Hello'N/A
3Call $greeter('Alice')$greeting='Hello'Closure uses captured $greeting='Hello' and parameter 'Alice'Returns 'Hello, Alice!'
4Echo closure result$greeting='Hello'N/AHello, Alice!
💡 Execution ends after printing the greeting message.
Variable Tracker
VariableStartAfter Closure DefinitionAfter Closure CallFinal
$greeting'Hello''Hello''Hello''Hello'
$nameN/AN/A'Alice'N/A
Key Moments - 2 Insights
Why does the closure still use the original value of $greeting even if $greeting changes later?
Because the closure captures $greeting by value at the time of definition (see step 2 in execution_table), later changes to $greeting do not affect the closure's copy.
What happens if we don't use 'use' to capture $greeting?
The closure won't have access to $greeting from the outer scope, causing an error or undefined variable inside the closure (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $greeting inside the closure when it is called?
A'Alice'
B'Hello'
Cnull
D'Goodbye'
💡 Hint
Check step 3 in the execution_table where the closure uses the captured $greeting.
At which step does the closure capture the variable $greeting?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the action 'Define closure' in the execution_table.
If $greeting was changed to 'Hi' after defining the closure but before calling it, what would the closure output?
A'Hi, Alice!'
BAn error
C'Hello, Alice!'
D', Alice!'
💡 Hint
Refer to variable_tracker and key_moments about capturing by value.
Concept Snapshot
Closure syntax: function(...) use ($var) { ... }
The 'use' keyword copies variables from outer scope by value.
Closure keeps original values even if outer variables change later.
Useful for creating functions with preset data.
Call closure like normal function with parameters.
Full Transcript
This example shows how PHP closures can capture variables from their surrounding scope using the 'use' keyword. First, we set a variable $greeting to 'Hello'. Then we define a closure $greeter that takes a name and uses $greeting from outside. The closure captures $greeting by value at definition time. When we call $greeter('Alice'), it returns 'Hello, Alice!'. Even if $greeting changes later, the closure still uses the original 'Hello' because it keeps its own copy. This is important to understand how closures bind variables in PHP.