Okay.
So next up let's learn the anonymous functions also called closures.
The main difference between the anonymous functions and the traditional functions is that the anonymous
functions don't exist under a specific name, which means that you need to assign this anonymous functions
either to a variable, or you need to pass it as a parameter to a function.
It can't really exist on its own.
That's the main conclusion.
And in PHP, like in many other languages, functions are called first class citizens.
What this means is exactly what I've described a second ago.
This means you can assign functions to variables or pass them around as function arguments.
Okay, so that's a little bit of theory.
Now let's jump to the code editor and let's see some examples.
Let's just call this anim PHP and let's see some anonymous functions also called closures.
So let's create an anonymous function.
And it will accept a name parameter.
And it will simply return the hello text with the name.
Now let me add a semicolon here.
And well how do we call this function.
Yes.
So that's the problem.
The anonymous functions or closures as they are also called.
They don't exist under a given name.
That's why you need to associate them with something like associate them with a variable like this greet
variable.
Now this variable is essentially the function name and to call it I can echo, greet and say maybe David.
Now let me call this, uh, this is anon PHP.
As you see, this has worked fine.
Okay.
So let's see yet another example.
This time we won't be associating the anonymous function to a variable.
Instead we're going to pass it to another function.
First things first, let's create some example data.
This would be this numbers array.
We're gonna calculate the square of every element in this array.
And the result will be put into a new array.
Let's call it squared.
Now we are gonna use an array map function.
This is a built in PHP function that will call our anonymous function on every item in the array.
So here we create an anonymous function in place and it's going to receive a number.
And we're just going to return the number times number.
This is how you calculate the square.
And the second argument to array.map is the source array.
This is our numbers.
So this time I'm gonna call Vardump.
So we also see the type.
And let's output squared and maybe the numbers the original array.
Okay.
Let me run it.
That's the original array 123 the numbers.
And here we've got the squares of every single number.
So this is another use case of anonymous functions, where you pass them to functions that work with
arrays or maybe strings, so that you can perform some operation in place.
And it wouldn't make a lot of sense to create a separate named function if what you are going to do
will be applied only in one specific instance, like in here.
Okay.
So now another thing.
So accessing variables in your anonymous functions that are outside the anonymous function itself.
For an example let's copy this fragment.
And here I don't want to actually say hello all the time.
I'd like to use a message that is not passed as an argument to this anonymous function.
Instead.
That's just a variable defined outside this anonymous function.
So it can be.
Hello.
Maybe I'd like to change it to by.
Like this.
Maybe let's also call this variable.
Otherwise.
Um maybe greet two didn't have any better idea.
Anyway, let's run that.
So PHP and PHP and we've got a warning that we are accessing an undefined variable message.
Yet here we can see it is defined.
So the thing with anonymous functions is that they don't have access to the variables defined outside.
That's just a thing to access them.
Well it's possible you can access the outside variables in your anonymous functions.
When you also add this use statement after a function, and here you list the variables you'd like to
be made available to your anonymous function.
So after I define this, that I want this message variable to also exist in the context of this anonymous
function.
This should now work.
And it does right.
Now let's also think about this variable.
So as the name suggests variable.
It's something that can change.
So you can change the value that variable holds.
So can we change this variable the message that we get access to inside this function.
Let's try that.
Maybe I can use the original value.
Concatenate that with an exclamation mark first and only then use it.
So let me run this anon fbx file.
As we see we had the exclamation mark, but after we have called this function, let's echo the message
and let's see if its value has changed.
Okay, let's run it again.
So what happened here is we've modified the message, and this was reflected in whatever was returned
from this anonymous function.
But the original message variable defined here didn't changed and contains the original value, the
original text that was set here.
So that's the thing with using the external variables in anonymous functions, the copy of this variable
is created that only exists in the context of this function.
So to actually be able to change the value of this variable.
If you would want that.
Obviously you need to pass this variable by reference using this end sign.
And then.
The change would also be made in the original variable.
So at this point the reference might be a foreign concept to you.
And I haven't explained it yet.
So now that we have learned enough about basic data types and functions, I think we should go back
to the variables and talk about the variables scope, because having to use this external variable is
the direct results of variables existing in certain scopes, and also passing by value and passing by
reference is something I would have to explain.
Let's talk about that next.