Anonymous function syntax in PHP - Time & Space Complexity
Let's see how the time needed to run an anonymous function changes as we use it in different ways.
We want to know how the number of steps grows when the function is called multiple times.
Analyze the time complexity of the following code snippet.
$numbers = [1, 2, 3, 4, 5];
$sum = 0;
$add = function($n) use (&$sum) {
$sum += $n;
};
foreach ($numbers as $num) {
$add($num);
}
This code defines an anonymous function to add numbers to a sum, then calls it for each number in the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the anonymous function inside a loop.
- How many times: Once for each item in the array.
Each time we add one more number, the function runs one more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to the function |
| 100 | 100 calls to the function |
| 1000 | 1000 calls to the function |
Pattern observation: The work grows evenly as the list gets bigger.
Time Complexity: O(n)
This means the time to finish grows directly with the number of items we process.
[X] Wrong: "Anonymous functions run slower and add extra hidden loops."
[OK] Correct: The anonymous function itself runs once per call, just like a normal function, so it doesn't add extra repeated work beyond how many times you call it.
Understanding how anonymous functions behave helps you explain your code clearly and shows you know how function calls affect performance.
"What if the anonymous function contained a loop inside it? How would the time complexity change?"
Practice
Solution
Step 1: Understand function naming
Anonymous functions do not have a name, unlike regular functions.Step 2: Recognize assignment to variables
They are often assigned to variables for later use or callbacks.Final Answer:
A function without a name, often assigned to a variable -> Option CQuick Check:
Anonymous function = unnamed function assigned to variable [OK]
- Thinking anonymous functions must have names
- Confusing anonymous functions with class methods
- Assuming anonymous functions run automatically
Solution
Step 1: Identify correct anonymous function syntax
In PHP, anonymous functions use the keywordfunctionwithout a name and are assigned to variables.Step 2: Check each option
$func = function() { echo 'Hello'; }; uses correct syntax:$func = function() { ... };. Others have invalid keywords or syntax.Final Answer:
$func = function() { echo 'Hello'; }; -> Option BQuick Check:
Anonymous function syntax = function keyword + no name + assignment [OK]
- Adding 'anonymous' keyword before function
- Using := instead of = for assignment
- Trying to name the anonymous function
$x = 5;
$func = function() use ($x) {
echo $x * 2;
};
$func();
Solution
Step 1: Understand 'use' keyword in anonymous functions
Theuse ($x)imports the variable$xfrom the outer scope into the anonymous function.Step 2: Calculate the output
The function echoes$x * 2, which is 5 * 2 = 10.Final Answer:
10 -> Option DQuick Check:
use imports variable, output = 5*2 = 10 [OK]
- Forgetting to use 'use' and expecting access to $x
- Expecting output 5 instead of 10
- Assuming variable is undefined inside function
$x = 10;
$func = function() {
echo $x;
};
$func();
Solution
Step 1: Check variable scope inside anonymous function
Variables outside anonymous functions are not accessible inside unless imported withuse.Step 2: Identify the error cause
The code tries to echo$xinside the function withoutuse ($x), causing an undefined variable error.Final Answer:
Variable $x is not accessible inside the anonymous function -> Option AQuick Check:
Outside variables need 'use' to be accessed inside [OK]
- Assuming outside variables are automatically accessible
- Thinking anonymous functions must have names
- Confusing syntax errors with scope errors
$a = 3;
$b = 4;
$sumFunc = function() {
echo $a + $b;
};
$sumFunc();
Solution
Step 1: Understand variable access in anonymous functions
Anonymous functions can access outside variables byuseor accept parameters.Step 2: Analyze options
$sumFunc = function() use ($a, $b) { echo $a + $b; }; usesuse ($a, $b)to import variables. $sumFunc = function($a, $b) { echo $a + $b; }; $sumFunc($a, $b); passes variables as parameters. Both are valid.Final Answer:
Both A and B are correct ways -> Option AQuick Check:
Use 'use' or parameters to access variables inside anonymous functions [OK]
- Forgetting to import variables or pass as parameters
- Assuming anonymous functions cannot take parameters
- Trying to access outside variables without 'use'
Ready to prove it?
Pass the test and this pattern moves unknown
