Why Create Functions 0:00You've learned a little bit about functions so far, but not too much. So you learned about HTML special chars, that's a function you can call. You learned about UC words. And we even visited php.net to see that there was a whole crop of built-in functions that you can call. But in addition to those native or built-in functions, you can create your own. And you're going to do this all the time. It's the bedrock of writing code in any language. A function allows you to take some kind of logic or operation and isolate it behind a key that can be triggered anywhere in your system after it has been defined. So why don't we do this? Debugging With Die Dump 0:33that can be triggered anywhere in your system after it has been defined. So why don't we do this? Do you remember before how we had, maybe a couple lessons ago, we had this animals array, dog and cat. And we wanted to var dump it, but we also wanted to kill the execution so that we didn't continue on and load the view in this case. So you learned that you could use the die function like this. Okay, so if we were to go back to Chrome, we die and dump this value. But then you also learned that if you're not pulling in any kind of plugin or extension to format it nicely, you could always wrap it within pretext.But then you also learned that if you're not pulling in any kind of plugin or extension to format it nicely, you could always wrap it within pretext. So if we come back and give that a refresh, now it looks a little better. And that's nice as a very first step. But here's the problem. Imagine a larger project where you keep wanting to do this. Well, it starts to become a pain, especially when this is temporary code. This is not code that lives in production, as you might say. It's just temporary code for you to debug the current state of the animals variable. So writing these three lines out over and over, we don't want that. Defining a Simple Function 1:33It's just temporary code for you to debug the current state of the animals variable. So writing these three lines out over and over, we don't want that. Instead, let's isolate it behind a function. Here's how we define a function. Function, we give it any kind of name. We accept some arguments or parameters. So 1, 2, 3. So this function would accept three parameters. And we assign each value to the variable name 1, 2, and 3. So why don't we do this?And we assign each value to the variable name 1, 2, and 3. So why don't we do this? Let's comment this out, like you learned. And we're going to var dump 1, 2, and 3. So what we have here is a very simple function. And we'll call it dumper. All it does is it accepts some parameters. And then it just dumps those to the page. Very, very simple. So if I were to call this and we said, hello, big world.Very, very simple. So if I were to call this and we said, hello, big world. Well, notice this value would correspond to the first parameter name, 1. Big would correspond to 2. And world would correspond to 3. Now, in this case, variable names of 1, 2, and 3, really bad choices. Remember a lesson or two ago where we talked about assigning readable variable names? That applies here as well. This is just an example. So if I go back to Chrome and we give it a refresh, there you go.This is just an example. So if I go back to Chrome and we give it a refresh, there you go. We've dumped hello, big, and world. So you just created your first function. But you did use really bad parameter names. And that's OK. In this situation, we're dumping strings. So it's not the worst thing ever. But why don't we make it a little more real life? We're going to change this to a function called dd. Building a dd Helper 3:18But why don't we make it a little more real life? We're going to change this to a function called dd. And I'm calling it dd because it's pretty common in the PHP world. So you'll use tools that have functions called dd. And that is shorthand for die and dump. So kill the execution and dump this value to the page. So if we die and dump this animals variable, we want to spit it out and basically get the exact same thing as what we had before. Fair enough. Let's take the code we had before and throw it into our functionFair enough. Let's take the code we had before and throw it into our function and add the necessary indentation. Now, in this case, it's not called dumper anymore. It's called dd. And it's only going to accept a value. So notice if we called it animals, this would work. So if we give it a refresh, yeah, that works. But it's not reusable, right? Well, I mean, it is reusable, but you're not always going to pass an animals array.But it's not reusable, right? Well, I mean, it is reusable, but you're not always going to pass an animals array. You might die and dump hello world. And if we come back to Chrome, you'll see it there. Good. But hello world is not animals. So you want to become more generic here. The function does not know what it's going to receive. So you want a generic name. In this case, what are you dumping?So you want a generic name. In this case, what are you dumping? You're dumping some kind of, well, in this case, value would be perfectly fine. You're dumping some kind of data, that would be fine as well, to the page. OK. So if we come back, we'll still get the same thing, of course. Because a variable name here, you can call it anything you want. You can say bippity boppity. And yeah, you're still going to get the same thing.You can say bippity boppity. And yeah, you're still going to get the same thing. The programming language does not care what you call this, but we care. You care as a developer, and your co-workers care as well. This is a terrible name, so we want to use something better and more appropriate, like data. So as always, think long and hard about how you name things. It's very important. And your future self will thank you. OK. Extracting to functions.php 5:14And your future self will thank you. OK. So good job. You've created your first function. And if you want, by the way, you could isolate that behind maybe a functions file, like this. Require. In fact, let's do this at the very top. Require functions.php. Once again, this is a good PHP 101 habit to get into. Later, as you learn more, you'll learn about more sophisticated ways to create functionsOnce again, this is a good PHP 101 habit to get into. Later, as you learn more, you'll learn about more sophisticated ways to create functions and things called classes. But for now, this is good. So create a file, functions.php. Come back. Move any reusable function you have, like this. But don't forget, notice we don't have highlighting here? Well, it's a PHP file, so you have to start it with PHP. Don't forget to do that.Well, it's a PHP file, so you have to start it with PHP. Don't forget to do that. All right. So you have required your functions file, and now you can use it anywhere after it has been defined. So if we run this, it'll still work. And if we die and dump the animals, it'll still work. Or you could inline the animals. Dog and cat. Yeah, it's all going to turn out exactly the same. Homework: Age Check Function 6:22Dog and cat. Yeah, it's all going to turn out exactly the same. OK. So good job. You learned how to create your first function. Here's your homework. Imagine you are building a website for a nightclub or something like that, and you want to give the user the option of checking to see if they're old enough to go into the nightclub. So it'll be very simple. If you are 21 or older, you are allowed.So it'll be very simple. If you are 21 or older, you are allowed. But if you're below that, you are not allowed. So your job is to create a function with a nice name, a nice readable name that accepts a value. That value will be the person's age. It could be 15, it could be 35. You can pass that in. Now, the function is going to use a conditional to check if that age is greater than or less than 21.Now, the function is going to use a conditional to check if that age is greater than or less than 21. And that's it. It returns a Boolean. That way, you can say within your code, if the function name, I'm not going to help you here, you're going to have to think of a good name there. So if the function name 15, well, if they can go into the nightclub, then proceed. Otherwise, proceed in another way. And maybe here you can just say, Echo, you are not old enough. Otherwise, Echo, come on in.And maybe here you can just say, Echo, you are not old enough. Otherwise, Echo, come on in. Anything you want there. So get started. Update your functions.php file and see if you can figure that one out on your own.