1 00:00:00,530 --> 00:00:03,700 Now that we've seen the initial structure of the function, 2 00:00:03,700 --> 00:00:06,980 the next step is to talk about function arguments. 3 00:00:06,980 --> 00:00:11,240 Information may be passed to functions through an argument list. 4 00:00:11,240 --> 00:00:13,850 The argument list is a comma delimited list of 5 00:00:13,850 --> 00:00:18,572 expressions that are evaluated from the left most item to the right. 6 00:00:18,572 --> 00:00:22,990 We will pass arguments into the function by placing them inside a parentheses, 7 00:00:22,990 --> 00:00:25,960 just after the function name definition. 8 00:00:25,960 --> 00:00:28,660 There are two methods of passing an argument in regards to 9 00:00:28,660 --> 00:00:32,100 how the argument value is affected by the function. 10 00:00:32,100 --> 00:00:37,030 The first is called passing by value, which is the default in PHP. 11 00:00:37,030 --> 00:00:40,290 In this case, the argument's value is not affected at 12 00:00:40,290 --> 00:00:43,980 all by the function outside of that function's scope. 13 00:00:43,980 --> 00:00:46,910 The other, which we'll not spend much time on in this course, 14 00:00:46,910 --> 00:00:48,970 is called passing by reference. 15 00:00:48,970 --> 00:00:53,350 Which means that the value of an argument, if modified by the function scope, 16 00:00:53,350 --> 00:00:57,370 will be modified on the outside of that function scope as well. 17 00:00:59,020 --> 00:01:03,260 Now let's take a look at function arguments inside of code. 18 00:01:03,260 --> 00:01:06,290 So, we'll create a function that we've already been using. 19 00:01:06,290 --> 00:01:11,250 So we'll do function, hello and then open and close. 20 00:01:11,250 --> 00:01:13,490 And then we're gonna have the same in the middle. 21 00:01:13,490 --> 00:01:15,420 So, the echo statement. 22 00:01:15,420 --> 00:01:17,680 And then the echo will be hello world. 23 00:01:20,550 --> 00:01:23,980 Okay, exclamation point, single quote and then semicolon. 24 00:01:23,980 --> 00:01:26,590 So, this is a function that we've had before. 25 00:01:26,590 --> 00:01:30,345 And we typically call this by saying just, hello and then open and 26 00:01:30,345 --> 00:01:32,720 close our parens and then semicolon. 27 00:01:32,720 --> 00:01:34,480 So let's double check this. 28 00:01:34,480 --> 00:01:36,750 We'll open it up, and it is Hello, World! 29 00:01:36,750 --> 00:01:37,820 Okay. 30 00:01:37,820 --> 00:01:42,780 So, in order to use function arguments, what we're going to need to do is go up to 31 00:01:42,780 --> 00:01:47,360 our hello and we're going to give an argument inside of our parentheses. 32 00:01:47,360 --> 00:01:53,510 The argument we want to pass through is dollar sign name, so $name. 33 00:01:53,510 --> 00:01:57,000 And in here we wanna echo that name instead of world. 34 00:01:57,000 --> 00:01:59,370 Well, if we just did dollar sign name here, 35 00:01:59,370 --> 00:02:02,900 that wouldn't work because we have to have it inside of double quotes. 36 00:02:02,900 --> 00:02:06,740 So, as long as I do this, now name will print to the screen. 37 00:02:06,740 --> 00:02:10,860 So in order to call this with name, we simply go down to hello and 38 00:02:10,860 --> 00:02:13,650 then pass through what we're expecting which is a string. 39 00:02:13,650 --> 00:02:14,920 So I'll say Hampton. 40 00:02:17,272 --> 00:02:21,760 All right, so if I save this file, go back over to my previous screen and 41 00:02:21,760 --> 00:02:25,452 refresh, now it says Hello, Hampton which is great. 42 00:02:25,452 --> 00:02:28,070 All right, go back over and let's say we wanna say a little bit more, 43 00:02:28,070 --> 00:02:32,500 so we'll just say Hello, and then How's it going? 44 00:02:34,620 --> 00:02:37,340 So it's very easy for us to update our function and 45 00:02:37,340 --> 00:02:40,470 then when we refresh, it'll be the same thing, just with more text. 46 00:02:40,470 --> 00:02:43,560 We can also change really quickly the name. 47 00:02:43,560 --> 00:02:45,830 So we'll just say, Hello, Mike. 48 00:02:45,830 --> 00:02:47,130 Go back over and refresh. 49 00:02:47,130 --> 00:02:47,830 Now it says Mike. 50 00:02:48,955 --> 00:02:54,380 Okay, so another thing we can do here is maybe pass multiple arguments in 51 00:02:54,380 --> 00:02:57,140 by using an array. 52 00:02:57,140 --> 00:03:00,170 So what we'll do is use this exact same function but 53 00:03:00,170 --> 00:03:05,590 instead of just saying hello to a name, we'll expect it to be an array. 54 00:03:05,590 --> 00:03:09,510 So we'll just call it arr for arrays. 55 00:03:09,510 --> 00:03:14,810 Then we're going to use just the name variable inside of an array. 56 00:03:14,810 --> 00:03:16,750 Well, how do we know what we're gonna do? 57 00:03:16,750 --> 00:03:21,510 Let's just say that every single one of these items is going to be a name and 58 00:03:21,510 --> 00:03:23,560 we want to say hello to them. 59 00:03:23,560 --> 00:03:27,740 So, the first thing we're gonna need to do is make sure that it actually is an array. 60 00:03:27,740 --> 00:03:29,750 So that's very easy with a conditional statement. 61 00:03:29,750 --> 00:03:35,490 So we'll do if, and then we'll say is, which is an internal function, array. 62 00:03:35,490 --> 00:03:42,030 So if is array, and then we pass through our actual variable, arr, or argument. 63 00:03:42,030 --> 00:03:48,060 And if it is an array, then we're gonna go and do something with that data. 64 00:03:49,110 --> 00:03:50,320 else, if it's not, 65 00:03:50,320 --> 00:03:56,640 what we're just gonna do is skip it completely and just say Hello, Friends. 66 00:03:56,640 --> 00:04:01,630 So we'll do, echo and then Hello, Friends. 67 00:04:02,830 --> 00:04:05,560 Close that with a semicolon and that should take care of it. 68 00:04:05,560 --> 00:04:09,090 Now what we're gonna do is cut this line for right now. 69 00:04:09,090 --> 00:04:16,370 And now inside of the if statement, we actually want to loop through our array. 70 00:04:16,370 --> 00:04:20,790 So we do that with a previous statement that we've learned which is the foreach. 71 00:04:20,790 --> 00:04:28,670 So foreach, and then array, as, and then $name. 72 00:04:28,670 --> 00:04:32,630 Then we can go inside by adding our curly braces, and 73 00:04:32,630 --> 00:04:34,545 then paste in our previous code. 74 00:04:34,545 --> 00:04:36,480 Tab that in for cleanliness. 75 00:04:36,480 --> 00:04:39,376 And now, it will actually echo Hello, and 76 00:04:39,376 --> 00:04:43,610 then use the $name here from line five, and then pass that through. 77 00:04:43,610 --> 00:04:46,630 So, Hello, name, how's it going? 78 00:04:46,630 --> 00:04:48,150 Let's see if this works. 79 00:04:48,150 --> 00:04:50,390 Right now it wouldn't because it would just say, 80 00:04:50,390 --> 00:04:52,560 Hello, friends, because it's not an array. 81 00:04:52,560 --> 00:04:54,930 Let's actually see how that degradates. 82 00:04:54,930 --> 00:04:55,800 See if it does nicely. 83 00:04:55,800 --> 00:04:57,210 So we'll go ahead and hit the Preview button. 84 00:04:57,210 --> 00:05:00,460 And then you see it says, Hello, friends. 85 00:05:00,460 --> 00:05:05,310 So we'll go back over and now instead of doing just Mike here, 86 00:05:05,310 --> 00:05:08,620 let's say we'll create a names array. 87 00:05:08,620 --> 00:05:12,540 So we'll do names equals and then we'll say array. 88 00:05:12,540 --> 00:05:17,850 And then inside that array, we'll close it out first. 89 00:05:17,850 --> 00:05:19,890 And here we're gonna have a couple of names. 90 00:05:19,890 --> 00:05:24,470 So we'll do Hampton and we'll do a comma on the next line just for 91 00:05:24,470 --> 00:05:26,100 cleanliness, we'll say Mike. 92 00:05:29,180 --> 00:05:32,660 And we'll do one more, Charley. 93 00:05:32,660 --> 00:05:36,980 Okay, so we'll save that and then go back to our Preview and hit Refresh. 94 00:05:36,980 --> 00:05:40,710 Right, so you can see it's doing Hello, Hampton, Hello, Mike and Hello, Charley. 95 00:05:40,710 --> 00:05:45,650 Of course there's no new lines after each one, so what I'll do is come up here to my 96 00:05:45,650 --> 00:05:52,090 echo statement and I'll just add a HTML break tag here, and save that. 97 00:05:52,090 --> 00:05:54,090 Let's see if that fixes it for us. 98 00:05:54,090 --> 00:05:54,770 Perfect. 99 00:05:54,770 --> 00:05:57,820 So Hello, Hampton, Hello, Mike, and Hello, Charley. 100 00:05:57,820 --> 00:06:01,090 So there you can see, in code, how we can take an array of values, 101 00:06:01,090 --> 00:06:04,570 in this case name, and loop through them in a function. 102 00:06:04,570 --> 00:06:06,630 So, simply by calling Hello and 103 00:06:06,630 --> 00:06:11,550 then passing through an array, we now have multiple Hellos from a single function.