When arrow functions landed in ECMA 6, they allowed developers to be much more fluid with use of functions. Suddenly, you could have short-syntax anonymous functions that also inherited the parent scope.
PHP gained its own version of arrow functions in version 7.4, in 2019. The two key points are the same - i.e. that they allow for short-syntax function expression and inherit the parent scope - but they have some differences, too.
Let's take a look at the syntax, firstly. In PHP, arrow functions look broadly like this:
fn? That always has to be there. Not sure why the PHP designers thought that was necessary, but there you go.
You can still add type/return bindings if you like.
In PHP, the parentheses are a must. This is presumably to separate the argument from the non-negotiable
So this, for example, has no effect:
We can get round this in the usual fashion, by forcing the value to arrive by reference via
I hope you found this comparison helpful!
Did I help you? Feel free to be amazing and buy me a coffee on Ko-fi!