Anonymous functions (also called closures) are functions without a name. They are assigned to variables and are especially useful for short callbacks.
Anonymous Functions
Assign a function keyword directly to a variable — no function name needed.
Arrow Functions (PHP 7.4+)
A shorter syntax for simple single-expression functions: fn($x) => $x * 2
Closures with use
Anonymous functions can access variables from the parent scope using the use keyword.
Common Use Cases
- Callbacks for array_map, array_filter, array_reduce
- Event handlers
- Sorting with custom logic