در حال بارگذاری ...

Introducing Functions and Filtering0:00

Okay, welcome back, and congratulations for making it this far. Trust me, I know it's not easy, especially the first time around. Okay, so now in this video, this should be fun, I get to introduce you to functions, which you can sort of think of as verbs of the programming world. Functions are responsible for doing things. Let me give you a couple examples. So have a look at our books array once again, and you'll see that I've added just a couple things. First up, the homework from the last episode requested that you add a releaseYear to each book, and then we display it right down here within our loop.

First up, the homework from the last episode requested that you add a release year to each Book, and then we display it right down here within our loop. So I've done that. Next up, I've added a third Book here called The Martian that is also by Andy Weir. And yeah, the reason why I'm doubling up on authors here is because we're not just going to discuss functions in this video, but you will also learn about filtering. So yeah, imagine this will ultimately be some kind of book repository that you maintain. And in real life, there are hundreds or thousands of books that you want to present to the user. In this case, I'm hard coding it, so we'll keep it very simple. But yeah, one of the things a user will instantly need to do is filter the books according to

In this case, I'm hard coding it, so we'll keep it very simple. But yeah, one of the things a User will instantly need to do is filter the books according to maybe the release year or the author. So it would be nice if I could say, well, just give me only the books that were written by Andy Weir, because that's all I'm interested in right now. Okay, so how exactly would we do it? All right, I'll give you a couple ideas. Let's scroll down here. Here's our loop. And yeah, notice we're saying, all right, for each book says book, and then display

Conditional Filtering in Loop1:34

Here's our loop. And yeah, notice we're saying, all right, for each book says book, and then display a list item. But maybe we could also say, well, if the book was written by Andy Weir, only on that condition should I render a list item. Otherwise, skip it. Okay, here's what we could do. Open up php, and we could say, if the book's author, and I'm going to add a little typo here, but just come along for the ride. If it equals Andy Weir, and let's use the alternative syntax, only on that condition

Assignment vs Equality Operators2:31

And the issue is this equal sign. All right, so here's the deal. When we use a single equal sign, like I've done here, we are literally assigning a value. So this effectively says, all right, for this book's author key, make it equal to, or assign it to Andy Weir. So for example, let me show you something. If we also included the author at the very end. So let's say, echo out the book's author, like this. And if I switch back and refresh, yeah, notice that now every book is written by Andy Weir, which of course we know is incorrect.

do we proceed. Okay, so how do we check for a quality? We use, and it's a little weird, but we use ===. And once again, my font will make it look a little fancier. Just make sure you type that equal sign three times and you're good to go. So now watch what happens. If I come back and give it a refresh, we only show and render the books that were written by Andy Weir. And in this case, we ignored Do Androids Dream of Electric Sheep because it wasn't written by Andy.

And in this case, we ignored Do Androids Dream of Electric Sheep because it wasn't written by Andy. So yeah, a single = sign when you want to assign a value, triple === when you want to check aquality. Okay, so I'm feeling a little better. This does work. But yeah, we are hard coding the author's name, which isn't overly practical, but yeah, it works. Instead, though, it would be nice if I could create a function that filtered the books automatically.

Defining and Calling Functions4:19

Instead, though, it would be nice if I could create a function that filtered the books automatically. And yeah, don't forget, functions are like the verbs of the programming world. Their job is to be called, they then do something, and then often they return a result. Okay, here's how we define a function, and I'll do it right down here. We start with the keyword function, and then we give it a name. And just like defining a variable, you can name it anything you want. In our case, I want to filter by the author. So let's be a little on the nose, filter by author. Then we open and close a parentheses, and then we add our braces.

So let's be a little on the nose, filterByAuthor. Then we open and close a parentheses, and then we add our braces. Okay, so we're starting to see a pattern here. Remember how we added braces when defining a conditional, and we learned that if that condition was truthy, if it evaluated to something that is positive, basically, only on that condition would we execute the logic within the braces. And the exact same thing is true for a function. This logic within here will only be executed when I actively call that function. And here's how we call a function. Reference its name, filterByAuthor, and then you can see my editor is helping me here.

And here's how we call a function. Reference its name, filterByAuthor, and then you can see my editor is helping me here. Open and close parentheses, and then we end our statement. This is my way of saying, call the function that is named filterByAuthor. Okay, so let's give this a shot. Let's play around for just a moment, and we'll say, maybe right down here, let's open a <p> tag, and to start, let's echo out whatever is returned from that function call. And yeah, take a moment and ask yourself what you think we're going to see here. Let's have a look in the browser. Give it a refresh, and I don't see anything.

Let's have a look in the browser. Give it a refresh, and I don't see anything. But if you think about it, that makes perfect sense. We're calling this function here. The function doesn't do anything at all, and it doesn't return anything at all, so of course we don't echo anything at all. That makes sense. Okay, so now let's have it return something. Gibberish. return, and notice I use this keyword here, return.

I could have loops. I could even interact with services outside of my application. And the coolest part is I get to isolate and abstract all of that often confusing logic behind a simple function with a readable name that describes what all of that code is doing. So that's sort of what we're doing here. We're just taking confusing logic and tucking it behind a function with a nice and readable name that describes what all of that logic is doing. Okay, so now think about it. Filter by author, well, that probably should receive something. It should receive all of the books here, and it should then filter them and return

Passing Arguments and Returning Data7:13

Filter by author, well, that probably should receive something. It should receive all of the books here, and it should then filter them and return only the books that were written by, in this case, Andy Weir. Okay, we have our work cut out for us. How do we do that? Well, the first thing I said is that the function needs to receive the books. So we can specify that a function requires a certain type of data by declaring it within the parentheses here. In this case, I need some books to filter. Okay, so now if I come back to the browser and I give it a refresh, immediately I get

In this case, I need some books to filter. Okay, so now if I come back to the browser and I give it a refresh, immediately I get an error. Too few arguments to function filterByAuthor. Okay, so php looked at this and it saw, okay, you have this function here and it requires some books to work off of. But then you called the function and you never passed through the books, and that is an error, so we see it in the browser here. Okay, let's pass through the books. Come back and give it a refresh, and now, again, we haven't changed the logic, but we

For this video, the code we will write will be a little naive, maybe a little unsophisticated. And then in the next video, I will show you a dedicated array_filter function that will make the code a lot more terse. But yeah, this is a good first step. So you might think to yourself, all right, I have a list of books and I want to return a new list of books that were only written by Andy Weir. Okay, so we might say, well, create a new array called filteredBooks. Then we could say, loop over the books that you gave us. We know how to do that. And then check the author of the book.

We know how to do that. And then check the author of the book. All right. Well, if that book's author is triple equal Andy Weir, in that case, add that book to our new array. We can add or append to an array using this syntax here. So notice the brackets here, append this book as a new item within this array. Okay. But now what if the author of the book is not Andy Weir? Well, in that case, we don't do anything at all.

Let's try this out. So right down here, where we loop over all of the books, let's change it. Let's now loop over only the books by Andy. So I could say filterByAuthor. And in this case, PHPStorm auto-completes for me. But yeah, again, notice how we call the function, we send through the data it requires. That triggers this function here, which means the logic between the braces runs. And in this case, it builds up an array, and it returns that array. So now we are looping over what was returned from this function call. All right, let's give this a run.

Parameterizing the Author Filter10:32

So now we are looping over what was returned from this function call. All right, let's give this a run. Back to Firefox, give it a refresh, and congratulations. It took a little work, but now we are successfully filtering an array according to the author. So now to finish up, last little thing, I don't see any reason to hard-code the author's name. Why don't we make that a parameter as well? So now we could say, all right, we're going to filter by author, but which author? Well, you have to give us that information. So we'll do that here.

Well, you have to give us that information. So we'll do that here. author is now a variable. Could be Andy Weir, or it could be the author of any book in our database. Okay, so all we have to do is swap out the hard-coded string with our parameter like this. Okay, so now notice we made a change, and my editor picks up on it, and it says, hey, you forgot to provide the author. Let's do it now. Once again, Andy Weir, and this is a nice little refactor.

FunctionsArray FilteringChecking for Equality

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟