1 00:00:00,000 --> 00:00:04,605 [MUSIC] 2 00:00:04,605 --> 00:00:07,810 In PHP functions are everywhere. 3 00:00:07,810 --> 00:00:12,560 Put simply a function is a way for us to organize and group statements of code. 4 00:00:12,560 --> 00:00:15,560 This code can then be wrapped with a name that will allow us to 5 00:00:15,560 --> 00:00:18,460 call this same group of code over and over. 6 00:00:18,460 --> 00:00:21,379 Functions can be described with the following analogy. 7 00:00:22,390 --> 00:00:25,620 A coffee pot would be a function called make coffee. 8 00:00:25,620 --> 00:00:28,400 The coffee pot takes coffee grounds and water. 9 00:00:28,400 --> 00:00:31,710 Then it heats the water and pours the water over the coffee, 10 00:00:31,710 --> 00:00:35,820 finally, through a filter, returning a cup of liquid coffee. 11 00:00:35,820 --> 00:00:40,520 Functions in most cases will be returning a value based off of supplied input, 12 00:00:40,520 --> 00:00:42,630 such as arguments, known values, 13 00:00:42,630 --> 00:00:46,140 like databases, external APIs, or date and time. 14 00:00:47,480 --> 00:00:51,860 To start with we're gonna open up a standard set of PHP tags, 15 00:00:51,860 --> 00:00:54,610 just like we're used to in all the previous lessons. 16 00:00:54,610 --> 00:00:57,980 Then we're going to use a new keyword called function. 17 00:00:57,980 --> 00:01:02,370 The keyword function will signal the PHP interpreter to tell it 18 00:01:02,370 --> 00:01:05,340 that anything after this is going to be a function. 19 00:01:05,340 --> 00:01:08,940 The next thing we pass after function, is a word hello, 20 00:01:08,940 --> 00:01:11,080 which is the name of our function. 21 00:01:11,080 --> 00:01:16,410 We open and close parentheses, and then we open and close our curly braces. 22 00:01:16,410 --> 00:01:18,540 Now inside of these curly braces, 23 00:01:18,540 --> 00:01:22,360 we're gonna do some sort of statement, some sort of PHP work. 24 00:01:22,360 --> 00:01:28,020 In this case, we're going to simply echo a string, and the string is Hello, World! 25 00:01:28,020 --> 00:01:31,480 Make sure you close it with a semicolon, and then we move on to the next step. 26 00:01:32,580 --> 00:01:35,090 This is the way we call our function. 27 00:01:35,090 --> 00:01:36,730 See, in order to call the function, 28 00:01:36,730 --> 00:01:40,460 we just simply place the name on a line by itself. 29 00:01:40,460 --> 00:01:43,420 And then open and close parentheses to let the interpreter know that 30 00:01:43,420 --> 00:01:45,765 we're calling a function named hello. 31 00:01:45,765 --> 00:01:48,400 We'll close that statement with a semicolon, and 32 00:01:48,400 --> 00:01:52,896 then the output, when this is run should be, Hello, World! 33 00:01:52,896 --> 00:01:57,000 All right, so in our workspace here we 34 00:01:57,000 --> 00:02:01,880 have an index.php with a PHP tag opened up for us to start with. 35 00:02:01,880 --> 00:02:06,860 So to create a function we're going to start by using the keyword function. 36 00:02:06,860 --> 00:02:13,940 So go down a line and type function, all right, followed by the function name. 37 00:02:13,940 --> 00:02:16,310 And in this case we'll call our function, hello. 38 00:02:16,310 --> 00:02:20,250 All right, we're gonna open and close the set of parentheses. 39 00:02:20,250 --> 00:02:23,560 There aren't gonna be any arguments, we'll get into that a little bit later. 40 00:02:23,560 --> 00:02:26,300 Then open and close our curly braces. 41 00:02:26,300 --> 00:02:28,060 Now anything inside of here, 42 00:02:28,060 --> 00:02:31,730 is what's going to happen when we call the function hello. 43 00:02:31,730 --> 00:02:34,156 So we'll tab in and echo a statement. 44 00:02:34,156 --> 00:02:39,310 So do echo and Hello, World! 45 00:02:39,310 --> 00:02:44,031 And close it with another single quote in the string and 46 00:02:44,031 --> 00:02:46,920 then a semicolon to end the line. 47 00:02:46,920 --> 00:02:49,450 Now that will actually create our function for 48 00:02:49,450 --> 00:02:54,190 us, but in order to use the function we have to call the function. 49 00:02:54,190 --> 00:02:55,890 So in order to call a function, 50 00:02:55,890 --> 00:02:59,600 we simply type the function's name followed by parentheses. 51 00:02:59,600 --> 00:03:03,550 So we'll type in hello, and then open and close parentheses. 52 00:03:03,550 --> 00:03:04,500 Now it is PHP, 53 00:03:04,500 --> 00:03:09,030 so we're gonna need to close this line with a semicolon to end the statement. 54 00:03:09,030 --> 00:03:09,890 Okay, let's go ahead and 55 00:03:09,890 --> 00:03:14,380 save this, and then we will ease on over to the eyeball for preview. 56 00:03:14,380 --> 00:03:20,210 And we will see now that it says hello world, which is exactly what we want. 57 00:03:20,210 --> 00:03:24,800 All right, let's switch back over to our code and we will create a new example. 58 00:03:24,800 --> 00:03:28,441 I'm gonna actually go ahead over and remove the line 7. 59 00:03:28,441 --> 00:03:29,414 We don't want to call it. 60 00:03:29,414 --> 00:03:32,244 We're gonna create another function, and 61 00:03:32,244 --> 00:03:35,260 this function is going to be called is_Mike. 62 00:03:35,260 --> 00:03:37,578 So let's say we'll do a function. 63 00:03:37,578 --> 00:03:41,580 And then is_Mike. 64 00:03:41,580 --> 00:03:46,040 Open and close parentheses, and open and close our curly braces. 65 00:03:46,040 --> 00:03:50,350 So in here, the idea behind this function is we're gonna have a variable. 66 00:03:50,350 --> 00:03:53,600 And that variable would be say, the currently logged in user. 67 00:03:53,600 --> 00:03:55,670 We'll call that variable current user. 68 00:03:55,670 --> 00:03:58,210 Now, in order to test to test to see if it's Mike, 69 00:03:58,210 --> 00:04:01,980 we're gonna need to bring in current user, and then do an if statement, and 70 00:04:01,980 --> 00:04:05,750 then output a string that tells us whether it is or is not Mike. 71 00:04:05,750 --> 00:04:09,560 So let's do that with starting with a conditional function, so, 72 00:04:09,560 --> 00:04:12,120 or a conditional if statement. 73 00:04:12,120 --> 00:04:16,526 So I'll do if, and then we'll say our variable is current_user, so 74 00:04:16,526 --> 00:04:19,000 current and underscore user. 75 00:04:19,000 --> 00:04:21,110 Equals, equals to make sure it's equivalent. 76 00:04:21,110 --> 00:04:22,065 And then, Mike. 77 00:04:22,065 --> 00:04:23,950 We'll close that with a single quote. 78 00:04:23,950 --> 00:04:28,100 Close our parentheses and now we have a completed if statement. 79 00:04:28,100 --> 00:04:32,430 If the current user is equal to Mike, we're gonna wanna output a string. 80 00:04:32,430 --> 00:04:37,471 So I'll do echo, and I'll say, It is Mike. 81 00:04:38,510 --> 00:04:40,260 All right. Close the single quote and 82 00:04:40,260 --> 00:04:42,680 then close the statement with a semicolon. 83 00:04:42,680 --> 00:04:47,860 So now if current_user is equal to Mike then we'll have a echo statement that 84 00:04:47,860 --> 00:04:49,590 says, It is Mike. 85 00:04:49,590 --> 00:04:54,100 Otherwise, so we need an else statement here and inside of our else statement, 86 00:04:54,100 --> 00:04:55,560 we'll say, it is not Mike. 87 00:04:55,560 --> 00:04:57,165 So, echo. 88 00:04:57,165 --> 00:05:03,780 Nope, it is not Mike. 89 00:05:03,780 --> 00:05:05,600 All right. I'll put a period in here, and 90 00:05:05,600 --> 00:05:07,790 then a semicolon to close the line. 91 00:05:07,790 --> 00:05:13,475 Now this is great, except when we call it, so we'll go is_mike. 92 00:05:14,790 --> 00:05:19,050 The only problem here is that we don't have a variable called, current_user. 93 00:05:19,050 --> 00:05:22,470 Now, there's something you need to understand about function scope. 94 00:05:22,470 --> 00:05:27,110 Any kind of variable that's used inside of the function here, 95 00:05:27,110 --> 00:05:32,210 so inside between lines 10 and line 14, 96 00:05:32,210 --> 00:05:36,670 anything inside that function is only for that function. 97 00:05:36,670 --> 00:05:39,190 So, in order to get something from the outside, 98 00:05:39,190 --> 00:05:42,760 we have to pass that through as an argument, or 99 00:05:42,760 --> 00:05:46,380 we can use the global statement, which we're gonna do now because we're gonna use 100 00:05:46,380 --> 00:05:50,530 something outside of the function scope, but we're not into arguments just yet. 101 00:05:50,530 --> 00:05:54,373 So, let's create this variable, current_user, and 102 00:05:54,373 --> 00:05:59,120 that current_user variable is gonna be equal to, to start off with, Mike. 103 00:06:00,742 --> 00:06:04,530 All right, now to get that inside of our function. 104 00:06:04,530 --> 00:06:09,710 We're gonna need to go inside the function and type global as our keyword, and 105 00:06:09,710 --> 00:06:10,535 then current_user. 106 00:06:12,800 --> 00:06:16,450 All right, now that will tell us we have a global variable outside of the function 107 00:06:16,450 --> 00:06:21,660 scope called current_user that we would like to use inside of our function. 108 00:06:21,660 --> 00:06:22,890 Okay, now that we've done that, 109 00:06:22,890 --> 00:06:26,680 let's run this code by going to our preview and see what it looks like. 110 00:06:26,680 --> 00:06:31,930 So I'm gonna go over to our, preview after I've saved it and then hit Refresh. 111 00:06:32,990 --> 00:06:35,168 It is Mike, which is correct. 112 00:06:35,168 --> 00:06:40,350 Now if we changed current_user to say, me, and then saved it, 113 00:06:40,350 --> 00:06:43,070 and then went back over and refreshed. 114 00:06:43,070 --> 00:06:44,530 It'll say Nope, it is not Mike. 115 00:06:44,530 --> 00:06:48,330 So now we have a function that runs, simply from a single line that we 116 00:06:48,330 --> 00:06:51,120 can find out whether or not the current user is equal to Mike.