Arrow functions in PHP. Code example open

Arrow functions in PHP. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

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.

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.

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation