External Variables of Arrow Function in PHP
In an arrow function, external variables are available automatically:
<?php
$num1 = 1;
$num2 = 2;
$func = fn() => $num1 + $num2;
echo $func(); // 3
?>
Rewrite the following code using an arrow function:
<?php
$num1 = 1;
$num2 = 2;
$func = function() use ($num1, $num2)
{
echo $num1 + $num2;
};
$func();
?>