Skip to content
Tutorials Center
My learning

Topics

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.

PHP
$rate = 0.2;
$withUse = function (float $n) use ($rate) { return $n * $rate; };
$arrow   = fn(float $n) => $n * $rate;

Try it

PHP Capture by value and by reference

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.

PHP Arrow function rules

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

PHP
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 returning 1 even after $x changes, and that incrementing inside a by-value closure does not alter the outer variable.
  • Arrow functions can't change outer variables. fn() => $total += 1 modifies only a local copy; use a closure with use (&$total) instead. Arrow functions cannot contain statements (no if, loops, echo as a statement), though match and ternaries work.
  • Arrow functions nest. The inner one sees the outer one's parameters, as $nested(2)(3) shows.
  • $this is bound automatically inside class methods. Mark the closure static if it does not need $this, so the object is not kept alive by it.
  • Rebinding. Closure::bind(), bindTo() and call() change $this and the class scope; see Closure.
  • Prefer arrow functions for short callbacks to array_map, usort and array_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 fn since 7.4.