An anonymous function is a function without a name that you can store in a variable or pass as an argument.
So far, we have always given names to our functions: function sumar().
But what if you need a small function just to use it once inside an array_map? Or if you want to save some logic inside a variable to execute it later?
Welcome to the world of Anonymous Functions (or Closures) and Arrow Functions.
When to use each one?
| Feature | Anonymous Function (function) | Arrow Function (fn) |
|---|---|---|
| Lines of code | Allows multiple lines and complex logic. | Only one expression (one line). |
| Return | You need to write return. | The return is implicit. |
| External variables | You need use ($var). | Automatic capture by value. |
| Ideal use | Complex logic, large blocks. | array_map, filter, quick callbacks. |
Anonymous functions (closures)
As the name implies, these are functions that have no name. They are assigned to a variable or passed directly as an argument to another function.
// Assign the function to a variable
$greeting = function($name) {
return "Hello $name";
}; // Note: It ends with a semicolon because it's an assignment.
// Execute it using the variable
echo $greeting("Luis");The scope problem and use
Remember what we said: functions in PHP are concrete walls. They don’t see what’s outside.
If you want to use an external variable inside an anonymous function, you have to “import” it explicitly using the keyword use. This is called a Closure.
$tax = 0.21;
// If you try to use $tax inside without 'use', it will fail.
$calculator = function($price) use ($tax) {
return $price * (1 + $tax);
};
echo $calculator(100); // 121This works, but… it’s a bit verbose, isn’t it? function, return, use, braces… too much syntax for a simple multiplication.
Arrow functions (fn)
To solve that verbosity, PHP 7.4 introduced Arrow Functions. They are the short and clean way to write anonymous functions.
They have two main characteristics:
- Ultra-short syntax: They start with
fn, use an arrow=>, and automatically return the value of the expression (you don’t need to writereturn). - Automatic variable capture: No need for
use! Arrow Functions can read outside variables automatically (by value).
Let’s look at the same previous example:
$tax = 0.21;
// Arrow Function version
$calculator = fn($price) => $price * (1 + $tax);
echo $calculator(100);It’s a single line! Clean, readable, and powerful.
The real use case: cleaning arrays
Where Arrow Functions truly shine is when combined with the array functions we saw in the previous lesson.
Imagine you have a list of products and you want to filter the expensive ones.
Old Style (Verbose):
$filter = array_filter($products, function($p) {
return $p['price'] > 100;
});With an arrow function:
$filter = array_filter($products, fn($p) => $p['price'] > 100);The difference in readability is immense. Modern code reads almost like a sentence: “Filter products where price is greater than 100”.
First-Class citizens (functions as data)
An advanced but important concept: in PHP, functions are “first-class citizens”. This means you can pass a function as if it were a number or a string.
function process(array $data, callable $operation) {
foreach ($data as $item) {
$operation($item); // Execute the function we were given
}
}
// Pass an Arrow Function as an argument
process([1, 2, 3], fn($n) => print "Number: $n \n");The callable type in the arguments ensures that what we receive is executable.