Arrow functions make it easier to write anonymous functions that return some value. And at the same time, arrow functions automatically have access to variables from the external environment.
Arrow functions in PHP 7.4 and later versions are a shortened syntax for creating anonymous functions.
Anonymous functions in PHP. Code example
May 4, 2022
An arrow function is defined using the fn operator:
fn(options) => actions;
Example:
$a = 5; $b = 5; $plus = fn($c) => $a + $b + $c; $result = $plus(22);
Arrow functions automatically capture variables from the surrounding field of view (closing).
Arrow functions can also be used as function parameters. Arrow functions make it easier to write simple functions and improve code readability. They are especially useful when a short function with one expression is required.