0
0
PHPprogramming~10 mins

Arrow functions (short closures) in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arrow functions (short closures)
Define arrow function
Use variables from parent scope
Return expression result
Call arrow function
Get returned value
Arrow functions are short, inline functions that automatically capture variables from the surrounding scope and return the result of a single expression.
Execution Sample
PHP
<?php
$multiplier = 3;
$double = fn($x) => $x * $multiplier;
echo $double(5);
?>
This code defines an arrow function that multiplies input by a variable from outside, then prints the result.
Execution Table
StepActionVariable ValuesExpression EvaluatedResult/Output
1Define $multiplier$multiplier=3--
2Define arrow function $double$double=fn($x) => $x * $multiplier--
3Call $double(5)$x=5, $multiplier=35 * 315
4Print result--15
💡 Function call completed and output printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$multiplierundefined3333
$doubleundefinedundefinedfn($x) => $x * $multiplierfn($x) => $x * $multiplierfn($x) => $x * $multiplier
$xundefinedundefinedundefined5undefined
Key Moments - 2 Insights
Why does the arrow function automatically know the value of $multiplier without passing it as a parameter?
Arrow functions automatically capture variables from the parent scope by value, so $multiplier is available inside without needing to pass it explicitly, as shown in step 3 of the execution_table.
What happens if you try to change $multiplier inside the arrow function?
Arrow functions capture variables by value, so changing $multiplier inside the function won't affect the outer variable. The execution_table shows $multiplier remains 3 throughout.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of $x inside the arrow function?
A3
Bundefined
C5
D15
💡 Hint
Check the 'Variable Values' column at step 3 in the execution_table.
At which step is the arrow function $double defined?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table to find when $double is assigned.
If $multiplier was changed to 4 after defining $double but before calling it, what would be the output at step 4?
A15
B20
C12
DError
💡 Hint
Arrow functions capture variables by value at call time, so changing $multiplier before calling affects the result.
Concept Snapshot
Arrow functions in PHP use fn() syntax for short closures.
They automatically capture variables from the parent scope by value.
Syntax: fn($params) => expression;
They return the expression result implicitly.
Useful for concise, inline functions.
Full Transcript
This visual trace shows how PHP arrow functions work step-by-step. First, a variable $multiplier is set to 3. Then, an arrow function $double is defined using fn($x) => $x * $multiplier, which captures $multiplier from outside. When calling $double(5), the function uses $x=5 and multiplies by $multiplier=3, returning 15. Finally, the result 15 is printed. The variable tracker confirms $multiplier stays 3, and $x is set only during the function call. Key points include automatic capturing of outer variables and implicit return of the expression. The quiz tests understanding of variable values during execution and effects of changing captured variables.