PHP Function Arguments6:11 with Hampton Paulk
Information may be passed to functions through an argument list. The argument list is a comma delimited list of expressions that are evaluated from the left most item to the right. Let's look at how we can use these arguments in our functions.
Now that we've seen the initial structure of the function, 0:00 the next step is to talk about function arguments. 0:03 Information may be passed to functions through an argument list. 0:06 The argument list is a comma delimited list of 0:11 expressions that are evaluated from the left most item to the right. 0:13 We will pass arguments into the function by placing them inside a parentheses, 0:18 just after the function name definition. 0:22 There are two methods of passing an argument in regards to 0:25 how the argument value is affected by the function. 0:28 The first is called passing by value, which is the default in PHP. 0:32 In this case, the argument's value is not affected at 0:37 all by the function outside of that function's scope. 0:40 The other, which we'll not spend much time on in this course, 0:43 is called passing by reference. 0:46 Which means that the value of an argument, if modified by the function scope, 0:48 will be modified on the outside of that function scope as well. 0:53 Now let's take a look at function arguments inside of code. 0:59 So, we'll create a function that we've already been using. 1:03 So we'll do function, hello and then open and close. 1:06 And then we're gonna have the same in the middle. 1:11 So, the echo statement. 1:13 And then the echo will be hello world. 1:15 Okay, exclamation point, single quote and then semicolon. 1:20 So, this is a function that we've had before. 1:23 And we typically call this by saying just, hello and then open and 1:26 close our parens and then semicolon. 1:30 So let's double check this. 1:32 We'll open it up, and it is Hello, World! 1:34 Okay. 1:36 So, in order to use function arguments, what we're going to need to do is go up to 1:37 our hello and we're going to give an argument inside of our parentheses. 1:42 The argument we want to pass through is dollar sign name, so $name. 1:47 And in here we wanna echo that name instead of world. 1:53 Well, if we just did dollar sign name here, 1:57 that wouldn't work because we have to have it inside of double quotes. 1:59 So, as long as I do this, now name will print to the screen. 2:02 So in order to call this with name, we simply go down to hello and 2:06 then pass through what we're expecting which is a string. 2:10 So I'll say Hampton. 2:13 All right, so if I save this file, go back over to my previous screen and 2:17 refresh, now it says Hello, Hampton which is great. 2:21 All right, go back over and let's say we wanna say a little bit more, 2:25 so we'll just say Hello, and then How's it going? 2:28 So it's very easy for us to update our function and 2:34 then when we refresh, it'll be the same thing, just with more text. 2:37 We can also change really quickly the name. 2:40 So we'll just say, Hello, Mike. 2:43 Go back over and refresh. 2:45 Now it says Mike. 2:47 Okay, so another thing we can do here is maybe pass multiple arguments in 2:48 by using an array. 2:54 So what we'll do is use this exact same function but 2:57 instead of just saying hello to a name, we'll expect it to be an array. 3:00 So we'll just call it arr for arrays. 3:05 Then we're going to use just the name variable inside of an array. 3:09 Well, how do we know what we're gonna do? 3:14 Let's just say that every single one of these items is going to be a name and 3:16 we want to say hello to them. 3:21 So, the first thing we're gonna need to do is make sure that it actually is an array. 3:23 So that's very easy with a conditional statement. 3:27 So we'll do if, and then we'll say is, which is an internal function, array. 3:29 So if is array, and then we pass through our actual variable, arr, or argument. 3:35 And if it is an array, then we're gonna go and do something with that data. 3:42 else, if it's not, 3:49 what we're just gonna do is skip it completely and just say Hello, Friends. 3:50 So we'll do, echo and then Hello, Friends. 3:56 Close that with a semicolon and that should take care of it. 4:02 Now what we're gonna do is cut this line for right now. 4:05 And now inside of the if stata, we actually want to loop through our array. 4:09 So we do that with a previous statement that we've learned which is the foreach. 4:16 So foreach, and then array, as, and then $name. 4:20 Then we can go inside by adding our curly braces, and 4:28 then paste in our previous code. 4:32 Tab that in for cleanliness. 4:34 And now, it will actually echo Hello, and 4:36 then use the $name here from line five, and then pass that through. 4:39 So, Hello, name, how's it going? 4:43 Let's see if this works. 4:46 Right now it wouldn't because it would just say, 4:48 Hello, friends, because it's not an array. 4:50 Let's actually see how that degradates. 4:52 See if it does nicely. 4:54 So we'll go ahead and hit the Preview button. 4:55 And then you see it says, Hello, friends. 4:57 So we'll go back over and now instead of doing just Mike here, 5:00 let's say we'll create a names array. 5:05 So we'll do names equals and then we'll say array. 5:08 And then inside that array, we'll close it out first. 5:12 And here we're gonna have a couple of names. 5:17 So we'll do Hampton and we'll do a comma on the next line just for 5:19 cleanliness, we'll say Mike. 5:24 And we'll do one more, Charley. 5:29 Okay, so we'll save that and then go back to our Preview and hit Refresh. 5:32 Right, so you can see it's doing Hello, Hampton, Hello, Mike and Hello, Charley. 5:36 Of course there's no new lines after each one, so what I'll do is come up here to my 5:40 echo statement and I'll just add a HTML break tag here, and save that. 5:45 Let's see if that fixes it for us. 5:52 Perfect. 5:54 So Hello, Hampton, Hello, Mike, and Hello, Charley. 5:54 So there you can see, in code, how we can take an array of values, 5:57 in this case name, and loop through them in a function. 6:01 So, simply by calling Hello and 6:04 then passing through an array, we now have multiple Hellos from a single function. 6:06
You need to sign up for Treehouse in order to download course files.Sign up