PHP Arrow Functions

Arrow functions were introduced in PHP 7.4 making them similar to JavaScript’s arrow functions.

See how $y is scoped on both functions.

$y = 1;

$fn1 = fn($x) => $x + $y;
// equivalent to using $y by value:
$fn2 = function ($x) use ($y) {
    return $x + $y;
};

Leave a comment

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

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.