0
0
PHPprogramming~10 mins

Closures in array functions in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Closures in array functions
Define array
Define closure function
Pass closure to array function
Array function calls closure on each element
Closure processes element and returns result
Array function collects results
Output final array
This flow shows how a closure (anonymous function) is defined and passed to an array function, which calls it on each element and collects the results.
Execution Sample
PHP
<?php
$numbers = [1, 2, 3, 4];
$squares = array_map(function($n) { return $n * $n; }, $numbers);
print_r($squares);
?>
This code uses a closure with array_map to square each number in the array.
Execution Table
StepArray ElementClosure InputClosure OutputCollected Result
1111[1]
2224[1, 4]
3339[1, 4, 9]
44416[1, 4, 9, 16]
Exit---All elements processed, array_map returns final array
💡 All elements processed, array_map returns final array
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
$numbers[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
$n (closure input)-1234-
Closure output-14916-
$squares[][1][1, 4][1, 4, 9][1, 4, 9, 16][1, 4, 9, 16]
Key Moments - 3 Insights
Why does the closure receive each element of the array one by one?
Because array_map calls the closure on each element individually, as shown in the execution_table rows 1 to 4.
What happens if the closure does not return a value?
array_map will collect null for that element, so the output array will have nulls in those positions.
Is the original array modified by array_map?
No, the original array ($numbers) stays the same, array_map returns a new array ($squares) as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the closure output when the array element is 3?
A9
B6
C3
DNone
💡 Hint
Check step 3 in the execution_table where Array Element is 3.
At which step does the collected result first contain the value 4?
AAfter step 1
BAfter step 3
CAfter step 2
DAfter step 4
💡 Hint
Look at the Collected Result column in execution_table after each step.
If the closure returned $n + 1 instead of $n * $n, what would be the collected result after step 4?
A[1, 4, 9, 16]
B[2, 3, 4, 5]
C[1, 2, 3, 4]
D[0, 1, 2, 3]
💡 Hint
Think about adding 1 to each element instead of squaring it, then check variable_tracker for original values.
Concept Snapshot
Closures in array functions:
- Define an anonymous function (closure) inline.
- Pass it to array functions like array_map.
- The closure runs on each array element.
- array_map collects and returns new array of results.
- Original array stays unchanged.
Full Transcript
This visual trace shows how closures work with array functions in PHP. We start with an array of numbers. We define a closure that squares a number. We pass this closure to array_map, which calls it on each element one by one. Each closure call takes the element as input and returns its square. array_map collects these results into a new array. The original array remains unchanged. The execution table tracks each step, showing input, output, and collected results. The variable tracker shows how variables change after each step. Key moments clarify common confusions about closure inputs, return values, and array immutability. The quiz tests understanding by referencing the execution steps and variable states.