Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last movie, we learned how to define and to call functions. In this movie, we'll get a better understanding about how we go about working with arguments to our functions. Let's create ourselves a new file this, and open up Basic.html. And we'll do Save As, and this one is going to be called functions_arguments.php. Then let's change this up here to be Functions: Arguments. And let's start out with just the basic say_hello_to function that we used in the last movie, which has one argument, word. Right? So we've already worked with arguments before, we've seen one.
Here it is, we've defined a variable, word, that we know we're going to pass a value into. We pass in the string, Everyone, to that. And that Everyone gets assigned to word. And that's what's available for use inside the function then. We already know how that works. I just want to show you a slight revision of that. Let's say that we have another variable here called, John Doe. And instead of passing in Everyone. Well we're going to pass in the variable name. So name equals John Doe. Say_hello_to name.
And that gets then passed into the function. And it should say Hello to John Doe. Let's try it. Let's go back to FireFox and instead of Functions Defining, it's going to be Function Arguments. Hello John Doe. Works exactly as we expected. And we're passing in a variable, but a variable is just a reference to a string. So it shouldn't be a surprise to us that this works. But I want you to notice that even though the variable is name outside the function, the function assigns that to the variable word. And word is what it uses inside the function.
These two don't have to match. So don't worry about that. Whatever it has out here. That's the one that exists outside the function. The function has its own sort of contained world of variables. We'll talk more about that a little later on. For now, I just want you to see the two don't have to have a relationship. Now, let's try some functions with more then one argument. If you remember back when we used the built in PHP function str_replace, we had three arguments to it. The first was the string quick, the second was super fast, and the third was a string That we assigned to the variable third.
Now, of course, this is already predefined for us by PHP. But if we were to define our own, it would look something like this. Function, str_replace, and then the three arguments, right, that go with it. And those might be find, replace, and the target. So we're finding one string. We're going to replace it with another string. Adn we're going to do that inside the target string. That's the third one. Now, it doesn't matter what this function does. What the statements are inside of it. What I want you to see here is the relationship between those three when we're defining the function and the three when we're calling the function. Notice that there are always three of them.
That fit each of those spots, and they're always in the same order. That matters when we're having multiple arguments. The order matters, and the number matters. So let's try our own. Let's start down here, I'll create new PHP tags, and let's put in function. We'll call this one, better_hello. And for now, I'll just flush it out like that. All right, so let's have three arguments here to this. This one will be greeting, and then we're going to pass in the target, and then the punctuation which I'll just abbreviate punct.
And then, I can use those three values inside my function as I go about the work of my functions. I'm going to say echo back the greeting. And then let's concatenate that with a space. And then we'll put the target, that's the person that we're greeting. And then followed by, concatenating that with the punctuation. And last of all, put br tag at the end, just to make it look nice, and a semicolon for the whole thing. See how that works? So we have three different ones that we are working with. Now, these don't have to be in this order, we could jumble these around.
The important thing though is that whatever order we define them in, that that's the order we called them in. We must always call it with the same number of arguments and in the same order. So better_hello and then we can now say that our greeting is going to be Hello. We'll say hello to the person that is stored in the variable name. And also, we're going to have our exclamation point as our punctuation. Alright, so let's just try this out and make sure that our three arguments are all working. There we go, we get Hello John Doe again. And of course, it's flexible. We could just as easily have changes from the greeting and the three exclamation points after it.
Right? Greetings John Doe with three exclamation points is what comes back. Now, as I said, we have to have the same number of items but let's try putting in null here for that last item. So we're providing a value. But that value is null. Let's come back over and reload the page and you'll see what it did. It converted null into a string, which is nothing. We could have also just as easily provided an empty string and it would have done the same thing. The important thing is that we have something there in that place. That if we are asking for three things in, when we define it, that we are sending three things whenever we call it.
There has to be agreement there. A little later, we'll talk about how we can set default values for these arguments. So that we don't always have to send the exact same number. But before we do that, I want us to first talk about return values from a function. We'll do that in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101728 Viewers
61 Video lessons · 88489 Viewers
71 Video lessons · 72307 Viewers
56 Video lessons · 104022 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.