Skip to content
Justice Tech School

Arrow Functions and Closures

fn creates short functions that capture outer variables by value. function () use ($x) is the long form.

Example
$k = 3;
$f = fn($x) => $x * $k;
$g = function ($x) use ($k) { return $x + $k; };
echo $f(2), " ", $g(2);

Result

6 5

Exercise

Short function.

$f = ($x) => $x * 2;

Quiz Test

How does fn get outer variables?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.