Anonymous functions and callables
What you'll learn:
- store a function in a variable and pass it to another function through a
callableparameter; - capture outer variables with
useand tell a copy of the value fromuse (&$x); - write short arrow functions with
fnand make a closure from an existing function withstrlen(...); - sort an array by your own rule with
usortand the<=>operator.

In the first theme, a function was a recipe card: the main recipe pointed to the cream card, the cook followed it and came back with the result. Such a card stays in its place, and you call it by name.
Now picture a card you can pick up and hand to someone else. The pastry chef does not know in advance which filling you want. You hand them the recipe card, and they cook from it.
PHP lets you treat a function the same way: put it in a variable, pass it to another function and call it there. Let's start with a function in a variable:
<?php
$double = function (int $n): int {
return $n * 2;
};
echo $double(4), "\n"; // 8
echo $double(10), "\n"; // 20
echo gettype($double), "\n"; // objectLet's walk through the program line by line:
function (int $n): int { … }is a function without a name. It has a parameter, a return type and a body, like an ordinary function.$double = …;puts this function into the variable$double. The semicolon after}ends the assignment.$double(4)calls the function stored in the variable: the parentheses with the argument come right after the variable's name. The call returns8.echo gettype($double)printsobject: a function in a variable is a special kind of value, an object of the built-in classClosure. Classes and objects come in theme 11; for now it is enough that such a value can be called.
A function without a name
An anonymous function is a function without a name, written where a value is needed. You can assign it to a variable, pass it as an argument or return it from another function with return.
It is written differently from a named function:
- right after the word
functioncome the parentheses with the parameters, and there is no name; - the whole thing is an expression, so an assignment needs
;after}.
There is also a difference in timing. PHP learns a named function before the run, so a call may come above the declaration. An anonymous function appears only when the line with the assignment runs, so calling $double above that line ends in an error.
This is where the card picture is off. To give a real card to two people, you would have to copy it. But $g = $double makes no copy: both variables hold the same Closure object.
A callable parameter
Now let's pass a function to another function. The function apply takes a function $f and a number $x and returns the result of calling $f($x):
function apply(callable $f, int $x): int
{
return $f($x);
}
echo apply($double, 7), "\n"; // 14
echo apply(function (int $n): int {
return $n + 1;
}, 7), "\n"; // 8Let's go step by step:
callable $fis the parameter's type: only something that can be called fits here.- Inside
apply, the call$f($x)runs the function it received with the argument$x. apply($double, 7)passes the function from the variable without parentheses. It is not called right away but goes intoapply, which returns14.- In the second call, the anonymous function is written right inside the call's parentheses.
applycalls it with7and returns8.
The function apply decides on its own when to call $f, like the pastry chef cooking from your card. A value that can be called with parentheses is a callable.
The callable type lets only callables into the function. If you pass a number to apply, PHP stops the program with a TypeError before the first line of the body.
A function's name in a string
A string holding a function's name is a callable too.
Take the built-in function abs: it returns the absolute value of a number, its distance from zero. abs(-5) is 5, and abs(5) is 5 as well. Let's pass it to apply by name:
echo apply('abs', -5), "\n"; // 5
$name = 'strtoupper';
echo $name('tea'), "\n"; // TEA
var_dump(is_callable('abs')); // bool(true)
var_dump(is_callable('shout')); // bool(false)Here is what happens:
apply('abs', -5)passes the string'abs'. Thecallabletype lets it in because it names an existing function, and the call$f($x)insideapplybecomesabs(-5).$name('tea')calls the function whose name is in the variable, that isstrtoupper('tea').is_callablechecks whether a value can be called and returnstrueorfalse. The program has no functionshout, so the second check printsbool(false).
A string with a name is handy when the function you need already exists: you do not have to write an anonymous one that only calls it.
Common mistake
The semicolon after the } of an anonymous function is missing:
<?php
$double = function (int $n): int {
return $n * 2;
} // ✗
echo $double(4), "\n";Parse error: syntax error, unexpected token "echo"PHP keeps reading the assignment and meets echo instead of ;. A parse error keeps the whole file from starting: not a single line runs.
A named function takes no semicolon after }, so people often forget it here. The fix is };, as in the first example.
Key points
- An anonymous function is a function without a name; you put it in a variable and call it with parentheses:
$double(4). - An assignment needs
;after the}of an anonymous function. - A
callableparameter takes a callable, and the function runs it as$f($x). - A string with a function's name, such as
'abs', is a callable too;is_callablechecks a value.