So what you are seeing right now is a syntax for the arrow functions.
And they're also known as short closures.
So this might ring a bell.
We've already talked about closures in PHP and this was anonymous functions.
Functions without a name that are passed as an argument to a function or associated with a variable.
So those arrow functions were introduced in PHP 7.4.
That's just a more concise way if you need to write short single expression functions.
So this is really important.
Arrow functions can only have one single expression.
And there is an implicit return.
What this means is in normal functions or the other functions than the arrow functions, whether that's
a closure or not.
To return something you need to use the return keyword here.
Well, you can do that.
But also whatever this expression evaluates to is returned from such function.
And well, the other things this function, this arrow function is not using the function keyword,
instead it is fn.
Then you have some optional arguments and instead of the curly braces you have this arrow symbol.
So those two characters and a semicolon.
After this single expression which is implicitly returned, let's now jump to the code editor and see
some actual examples.
So let's make an arrow PHP file, and the plan is to see an array of numbers being squared using an
array map function.
And we're going to have two examples.
So traditional anonymous function.
So you can see how much shorter the new arrow function syntax is.
So this is something super simple.
An array of numbers.
We've used that before.
Then a squared variable.
So we're going to use array map which is essentially like doing a foreach and creating a new array.
But instead of using the for each loop we can call this function the array map that accepts an anonymous
function like that from which we just return n times n.
And we also tell it that the input is this numbers array.
The result stored in squared would be a new array.
Let's just dump this array and also the original one so we can see the results.
So we run PHP or PHP.
There are the two arrays.
That's the original one.
And this is the squared one.
So I think that we have probably did this example before.
So there is nothing special to explain here.
Now let me show you the same example, but this time by using arrow functions.
So the first change is function is change to fn.
Then we just get this parameter.
There is no curly braces.
Instead there is this arrow symbol equality sign and more than placed together there is no return keyword
as arrow functions implicitly return the value.
Let's get rid of this curly brace.
And also in this case there is no need for a semicolon.
I can also move this to some separate lines so it's clearly visible.
This is how this new arrow function looks like.
Instead of this much shorter syntax, and very useful for cases like passing a closure to one of the
built in PHP functions for processing arrays and other different functions that you might not know yet.
Finally, let's assign this to squared two and just make sure that it works.
So there is the output same in both race, which means that it works normally.
Now that's not everything about the arrow functions.
Let me introduce a variable that would be global.
Let me call that multiplier.
So instead of squaring the values from this array we're gonna multiply it by the multiplier.
So let's first do it with this traditional closure.
I can do it like this.
Now you probably remember this won't work as this closure can't access a variable from the outside scope
and it just returns zeros everywhere and there should be a warning on the top.
The variable multiplier is undefined, so traditional closures can't access global variables variables
from outside its scope.
To do it, you need to use this use statement and only then use multiplier.
And then you can use this variable.
So you can see that this is getting really complex.
Now that's not the case with arrow functions as arrow functions as the arrow functions just inherit
the variables from the parent scope.
So arrow functions work out of the box with outside variables.
No need to add any use statements.
If I run this script right now, we get the output.
So everything is properly calculated and there are no issues whatsoever.
As I've said, you can just use outside variables with arrow functions.