0
0
PHPprogramming~10 mins

Anonymous function syntax in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Anonymous function syntax
Define anonymous function
Assign to variable or use directly
Call the function
Execute function body
Return or output result
End
This flow shows how an anonymous function is defined, assigned or used, called, executed, and then ends.
Execution Sample
PHP
<?php
$greet = function($name) {
    return "Hello, $name!";
};
echo $greet("Alice");
?>
Defines an anonymous function that greets a name, assigns it to $greet, then calls it with "Alice" and prints the result.
Execution Table
StepActionEvaluationResult
1Define anonymous function and assign to $greetfunction($name) { return "Hello, $name!"; }Variable $greet holds the anonymous function
2Call $greet with argument "Alice"$greet("Alice")Executes function body with $name = "Alice"
3Return string from functionreturn "Hello, Alice!";"Hello, Alice!"
4echo outputecho "Hello, Alice!";Prints: Hello, Alice!
💡 Function call completes and output is printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$greetundefinedanonymous function assignedfunction calledfunction returned stringanonymous function remains assigned
Key Moments - 3 Insights
Why do we assign the anonymous function to a variable?
Assigning the function to a variable like $greet lets us call it later by that name, as shown in step 2 of the execution_table.
What happens when we call $greet("Alice")?
The function runs with $name set to "Alice", returning "Hello, Alice!" as seen in steps 2 and 3 of the execution_table.
Can we use the anonymous function without assigning it to a variable?
Yes, but then we must call it immediately or pass it directly, which is different from the example where we assign it first (step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is stored in $greet after step 1?
AA string "Hello, $name!"
BThe result "Hello, Alice!"
CAn anonymous function
DUndefined
💡 Hint
Check the 'Result' column in row 1 of execution_table.
At which step does the function return the greeting string?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table row 3.
If we change the argument to "Bob", what changes in the execution_table?
AThe returned string changes to "Hello, Bob!"
BThe variable $greet becomes undefined
CThe function definition changes
DNo change at all
💡 Hint
Focus on the 'Evaluation' and 'Result' columns in steps 2 and 3.
Concept Snapshot
Anonymous functions are functions without a name.
Syntax: $var = function(params) { body };
Call like: $var(args);
Useful for quick, one-time functions.
Can be assigned to variables or used directly.
Full Transcript
This example shows how to create an anonymous function in PHP by assigning it to a variable named $greet. The function takes one parameter, $name, and returns a greeting string. When we call $greet("Alice"), the function runs with $name set to "Alice" and returns "Hello, Alice!". Then echo prints this result. The variable $greet holds the function itself, allowing us to call it multiple times if needed. This is a simple way to use functions without naming them explicitly.