PHP Reference ยท Language Constructs and Statements
PHP closures and arrow functions
Reference for PHP closures and arrow functions: function () use, by-value and by-reference capture, fn => auto-capture, static closures and when to use each.
PHP has two syntaxes for anonymous functions. A closure, written function (...) use (...) { ... }, can carry variables from the surrounding scope into its body through the use clause. An arrow function, written fn(...) => expression, is a short form for a single expression that captures outer variables automatically, by value.
Both create Closure objects, so you can store them in variables, pass them to functions such as array_map(), and call them like any other function.
$rate = 0.2; $withUse = function (float $n) use ($rate) { return $n * $rate; }; $arrow = fn(float $n) => $n * $rate;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
function (parameters) use ($by_value, &$by_ref): ReturnType { body } fn(parameters): ReturnType => expression static function () { ... } static fn() => ...
| Part | Description |
|---|---|
use ($a, &$b) |
Closures only. Copies $a when the closure is created and binds $b by reference. The copy is fixed at creation time. |
fn |
Arrow function. Variables used in the expression are captured by value at creation, automatically, only if used. |
| Body | A closure body is a block of statements with return. An arrow function body is one expression whose value is returned. |
static |
Prevents automatic binding of $this. |
Return value: a Closure object.
Errors: calling with too few arguments throws ArgumentCountError; a use variable that is $this or a superglobal is a compile error.
Usage notes
- By value means a snapshot. The first example shows that
$later()keeps returning1even after$xchanges, and that incrementing inside a by-value closure does not alter the outer variable. - Arrow functions can't change outer variables.
fn() => $total += 1modifies only a local copy; use a closure withuse (&$total)instead. Arrow functions cannot contain statements (noif, loops,echoas a statement), thoughmatchand ternaries work. - Arrow functions nest. The inner one sees the outer one's parameters, as
$nested(2)(3)shows. $thisis bound automatically inside class methods. Mark the closurestaticif it does not need$this, so the object is not kept alive by it.- Rebinding.
Closure::bind(),bindTo()andcall()change$thisand the class scope; see Closure. - Prefer arrow functions for short callbacks to
array_map,usortandarray_filter; use closures when you need a block of statements or by-reference capture. - Capture only what you need. Large arrays captured by value are copied lazily, but every captured variable still keeps its data alive for the closure's lifetime.
- Variables are captured when the closure is created, not when it is called. A variable that does not exist then is captured as
null, with a warning for closures. - Strings as callables such as
'trim'can be passed instead of closures; for methods and functions you may also use first-class callable syntax. - Arrow functions arrived in PHP 7.4; closures in 5.3;
static fnsince 7.4.