Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
PHP is a popular, reliable programming language at the foundation of many smart, data-driven websites. This comprehensive course from Kevin Skoglund helps developers learn the basics of PHP (including variables, logical expressions, loops, and functions), understand how to connect PHP to a MySQL database, and gain experience developing a complete web application with site navigation, form validation, and a password-protected admin area. Kevin also covers the basic CRUD routines for updating a database, debugging techniques, and usable user interfaces. Along the way, he provides practical advice, offers examples of best practices, and demonstrates refactoring techniques to improve existing code.
We've already learned that the arguments when you call a function must match the arguments that were used to define the function. If you define it with three arguments, then you need to provide three arguments as input. However, there is a way around that, by setting default argument values. Let's see how. So we'll open up basic.html. We'll call Save As on that. And let's call this functions_defaults.php. Functions default argument values. All right, so to start off with let's write ourselves a little function. And I'm going to call this function, paint.
Alright, so function paint and, let me just put the curly braces there. And this paint is going to take a color as an argument. The work that this function is going to do is we're going to return a string, which is the color of the room is, and then we'll have our curly braces with the color. And then I'll put a period and br tag at the end, followed by the semicolon. Okay. So now, we know how to call that, right? You can simply say echo paint and blue. Now, let's bring that up in a window and check it out. So here we go into Firefox and now set Function scope. Here we are looking at Defaults.
The color of the room is blue, right? Makes sense. We have one argument defined and we're passing in one thing. Now, if we were to take blue away, then we've got an error. It'll say, oops, sorry, missing argument one for paint. Right? And then, it also says, undefined color. Here, because we didn't pass anything in. So that won't work. We would need to pass in a placeholder, at least an empty string, something that it can work with. But if we use a default argument, the way that we do that is we do an assignment right after the argument.
So we just do equals, and then let's put in a color here. So let's say that the color's going to be red. All right. So now, let's just try this again. Reload. The color of the room is red. See what happens? It didn't get a value, so it get here and says color, and if you don't have a value for color, then go ahead use red. We can have more than one of these. Let's add another one, here. Let's say that we're going to have the room and the room, by default, is going to be the office, we need a comma between those, right? So room equals office, color equals red. So now, we need to pass in two things.
Let's also drop in the room here. So room and then let's come back over here. The color of the office is red. 'Kay, I can also pass in something else. I could have bedroom and blue. I'll just leave my old one as well, just so that we still have that paint. You can see the difference. So now I'm going to use bedroom and blue, and you'll see that we get the default behavior followed by our custom behavior. See how that works? Now, let's try one more. This time instead of passing in blue, let's pass in null and see what happens. Back here, color of the bedroom is null.
Even though it was null. All right. We did not pick up this red value. It did not go to the default. We passed in something. That something happened to be null, but it was something. The defaults only take place if nothing fills that spot. And I don't mean nothing as in null. I mean no value gets sent at all. Don't let that trip you up. Not sending something and sending nothing are not the same thing. Now, also, while it will work for you to just do bedroom and then provide nothing after it, all right. The color of the bedroom is red, picked up that value there. We can't do something like, blue, let's come back here, reload, the color of the blue is red.
That's what we got back, because we can't leave out this first one. The order still matters, and something has to hold that position. We can't put null here, because we know what that would do. So something has to hold that. So because of that, you going to want to make sure that any required values are going to be listed first. The optional ones, the ones that have defaults need to be listed. At the end of the argument list. Now, the argument order doesn't matter to the functions operation. It doesn't care what order they come in, as long as they match the way that it's called.
It can sort it out, it can swap color and room and it won't make any difference at all. But you would not to do something like this. All right, here color is required and room has a default value. Well, we've always got to provide something here. So this will never happen. We'll never get that office because something will always have to be provided here because this one is mandatory. That's all there is to working with default argument values. It's pretty simple to provide defaults, but there are some gotchas in there that can make it a little trickier than it first seems.
Find answers to the most frequently asked questions about PHP with MySQL Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
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.