‫So another way that we can use anonymous functions is to pass it as an argument to another function.
‫And this is called a callback function.
‫And it gets passed in and called back later on within the function that it was passed into.
‫And there's many built in PHP functions that use callbacks.
‫In fact, we've used some of them, including map, which allows you to apply a callback function to
‫each element of an array.
‫So just to kind of revisit that, let's go ahead and open up our PHP tags and we'll create a numbers
‫array.
‫Okay.
‫And then I'm going to create an anonymous I'm sorry, not an anonymous function.
‫I'm going to use the array map function and put that result into.
‫A variable called squared numbers.
‫Because what I want this to be is an array of those numbers squared.
‫So I'm going to set that to array underscore map.
‫An array map takes in a callback.
‫So we can go ahead and say function.
‫And we're not going to name it.
‫It's anonymous.
‫And that takes in whatever we want to call each item in the array.
‫So in this case number.
‫And then in here we can return the number and we want to square it.
‫So we're going to multiply it by number okay.
‫And then this takes in a second argument of the array which is numbers.
‫All right.
‫And then we can check this out.
‫Let's just use we'll do print R because it's an array.
‫And then squared numbers if we take a look.
‫We get one, four, nine, 16, 25.
‫So it's squaring these numbers here.
‫All right.
‫So this is another example of how you can use an anonymous function.
‫And you can put this into a variable and then pass the variable in.
‫That makes it a little more clear.
‫So for instance I could say let's say square and we'll set that equal to this function.
‫So I'm just going to cut that and then put that here I'm going to put a semicolon on the end.
‫And then instead of passing in the whole function I'll just pass in the square variable.
‫So if I do that and reload we get the same result.
‫Now of course array map is a predefined function, so we don't actually see the internal implementation
‫of it in this example.
‫But it accepts your basically your custom logic in the form of a callback function to process the array
‫elements.
‫Okay, we chose to square them.
‫But of course you could do whatever you want in that callback function.
‫So let's create our own function that takes in a callback function.
‫I'm just going to comment out that print R and then we'll say function.
‫And we'll just call it apply callback because that's what it's going to do.
‫And then we'll pass in a parameter that we'll just call callback.
‫So you'll see that.
‫Or you might see CB sometimes but we'll say callback.
‫And then let's say it also takes in a value.
‫And what I want this to do is simply return.
‫Callback, which is going to be a function, and I want to pass in that value.
‫All right.
‫And then down here we'll create the callback function to pass in.
‫And I'm going to call it double.
‫So we're going to set that to an anonymous function.
‫And it's going to take in a number.
‫And then what we're going to do is just return that number and we're going to double it.
‫So we're going to multiply it by two.
‫Okay.
‫So now I have my apply callback, which is what I'm going to call.
‫Right.
‫And then this double is what I want to pass in.
‫And that needs a semicolon.
‫So let's create a variable called result.
‫And then we'll call the apply callback.
‫And we're going to pass in the double function as the callback.
‫And then the value will pass in as five okay.
‫So we're passing this function in this right here.
‫We're passing it in.
‫And then it's going to return that callback with the value passed into it.
‫We're passing in five.
‫So that will get passed in as the number.
‫And then ultimately it's going to return the number which is five times two.
‫So we should get ten.
‫So let's go ahead and echo out the result.
‫Save that and reload.
‫And you can see we get ten.