Closures and Callables
Anonymous functions can capture variables with use. First class callable syntax strlen(...) creates closures from functions. call_user_func and is_callable work with callables.
Example
$tax = 0.2;
$withTax = function ($p) use ($tax) { return $p * (1 + $tax); };
echo $withTax(100), "\n";
$len = strlen(...);
echo $len("hello"), "\n";
$m = array_map(strtoupper(...), ["a", "b"]);
echo implode($m), "\n";Result
120 5 AB
Exercise
Capture an outer variable.
function () ($x) {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.