‫All right guys.
‫So in this video we're going to look at arrow functions which are introduced in PHP 7.4.
‫And they're a special type of anonymous lambda function.
‫And they allow us to write just one liner functions.
‫Very clear concise short.
‫And if you're familiar with JavaScript then you probably have used arrow functions in JavaScript.
‫And they're very, very similar.
‫So let's start off by just creating a very simple named function.
‫So I'll say function add.
‫And that's going to take in A and B.
‫And we're going to return from that a plus B.
‫Okay.
‫And then we'll just down here, we'll echo out.
‫Add and we'll put some numbers in there.
‫All right.
‫So if I reload I get three.
‫So the first step to change this to an arrow function is to get rid of the function name.
‫Because it's going to be an anonymous function.
‫Next we're going to create a variable to put this in I'll call it add.
‫Then we're going to take the function keyword and change that to just f n okay.
‫When you're using an arrow function you don't use function you use f n.
‫Then after the parentheses we want to add what's called a fat arrow.
‫And that's an equal sign and a greater than sign.
‫Then with arrow functions we don't use the curly braces.
‫So we'll get rid of the ending one.
‫We don't use the return keyword.
‫Get rid of that and get rid of that curly brace.
‫And that's it.
‫So you can see how much shorter that is.
‫If I reload, it's not going to work because I'm still calling the named function.
‫I just simply need to add a dollar sign here so that I'm calling the variable.
‫The function in the variable name.
‫And then I get three.
‫All right.
‫So those are the steps to take to make an arrow function.
‫Now if you are coming from JavaScript then you're probably used to doing this in so that you're able
‫to have multiple lines.
‫So you could do that.
‫And then you could just use the explicit return.
‫And we would need a semicolon here.
‫So that's not actually not going to work.
‫So in PHP we can't do this in JavaScript.
‫You can.
‫And then you can add more lines in PHP.
‫These are meant to just be one liners.
‫So they're good for like really small helper functions.
‫So we don't want to do that.
‫All right.
‫Now another good case for arrow functions is with callbacks.
‫So let's use the Array.map example that we used in a couple videos ago where we squared the numbers.
‫So we'll say numbers set that to an array.
‫And then let's create a variable called squared numbers.
‫And I just want to use array map to square each number.
‫And remember array map takes in a callback.
‫Okay.
‫So we'll just pass.
‫We'll do a regular function first.
‫So let's say function.
‫And then we're going to pass in here each item is going to be called number.
‫And then we're going to return whatever that number is.
‫And we want to multiply it by itself.
‫And then we have to pass in as a second argument here we want to pass in the array that we want to run
‫this on.
‫All right.
‫Then we could just do let's do a print.
‫Ah, because it's an array.
‫We can't echo it.
‫So we'll say squared numbers.
‫All right.
‫If I reload we get one, four, nine, 16, 25.
‫So it's taking each one of these mapping through calling this function on each one and squaring it.
‫So we can clean this up a little with an arrow function.
‫First off we'll change function to just fn.
‫Then we'll get rid of the ending curly brace right here.
‫Right here.
‫And then the semicolon.
‫Because since it's an argument, we don't want a semicolon right here.
‫So get rid of that and then get rid of the return and the other curly brace.
‫And we need to have an arrow here.
‫All right.
‫So that will do the same exact thing.
‫But it's shorter and cleaner.
‫So callbacks are a great use case for arrow functions.
‫All right.
‫Now in the next video I'm going to give you another challenge.
‫And I want you to change the format salary function from our job listing into an arrow function.
‫And you're also going to pass in pass that in as a callback to the calculate average function, and
‫I'll explain more in the next video.