Introducing Functions6:52 with Hampton Paulk
In PHP functions are everywhere. Put simply a function is a way for us to organize and group statements of code. Let's learn how these functions work.
[MUSIC] 0:00 In PHP functions are everywhere. 0:04 Put simply a function is a way for us to organize and group statements of code. 0:07 This code can then be wrapped with a name that will allow us to 0:12 call this same group of code over and over. 0:15 Functions can be described with the following analogy. 0:18 A coffee pot would be a function called make coffee. 0:22 The coffee pot takes coffee grounds and water. 0:25 Then it heats the water and pours the water over the coffee, 0:28 finally, through a filter, returning a cup of liquid coffee. 0:31 Functions in most cases will be returning a value based off of supplied input, 0:35 such as arguments, known values, 0:40 like databases, external APIs, or date and time. 0:42 To start with we're gonna open up a standard set of PHP tags, 0:47 just like we're used to in all the previous lessons. 0:51 Then we're going to use a new keyword called function. 0:54 The keyword function will signal the PHP interpreter to tell it 0:57 that anything after this is going to be a function. 1:02 The next thing we pass after function, is a word hello, 1:05 which is the name of our function. 1:08 We open and close parentheses, and then we open and close our curly braces. 1:11 Now inside of these curly braces, 1:16 we're gonna do some sort of statement, some sort of PHP work. 1:18 In this case, we're going to simply echo a string, and the string is Hello, World! 1:22 Make sure you close it with a semicolon, and then we move on to the next step. 1:28 This is the way we call our function. 1:32 See, in order to call the function, 1:35 we just simply place the name on a line by itself. 1:36 And then open and close parentheses to let the interpreter know that 1:40 we're calling a function named hello. 1:43 We'll close that statement with a semicolon, and 1:45 then the output, when this is run should be, Hello, World! 1:48 All right, so in our workspace here we 1:52 have an index.php with a PHP tag opened up for us to start with. 1:57 So to create a function we're going to start by using the keyword function. 2:01 So go down a line and type function, all right, followed by the function name. 2:06 And in this case we'll call our function, hello. 2:13 All right, we're gonna open and close the set of parentheses. 2:16 There aren't gonna be any arguments, we'll get into that a little bit later. 2:20 Then open and close our curly braces. 2:23 Now anything inside of here, 2:26 is what's going to happen when we call the function hello. 2:28 So we'll tab in and echo a statement. 2:31 So do echo and Hello, World! 2:34 And close it with another single quote in the string and 2:39 then a semicolon to end the line. 2:44 Now that will actually create our function for 2:46 us, but in order to use the function we have to call the function. 2:49 So in order to call a function, 2:54 we simply type the function's name followed by parentheses. 2:55 So we'll type in hello, and then open and close parentheses. 2:59 Now it is PHP, 3:03 so we're gonna need to close this line with a semicolon to end the statement. 3:04 Okay, let's go ahead and 3:09 save this, and then we will ease on over to the eyeball for preview. 3:09 And we will see now that it says hello world, which is exactly what we want. 3:14 All right, let's switch back over to our code and we will create a new example. 3:20 I'm gonna actually go ahead over and remove the line 7. 3:24 We don't want to call it. 3:28 We're gonna create another function, and 3:29 this function is going to be called is_Mike. 3:32 So let's say we'll do a function. 3:35 And then is_Mike. 3:37 Open and close parentheses, and open and close our curly braces. 3:41 So in here, the idea behind this function is we're gonna have a variable. 3:46 And that variable would be say, the currently logged in user. 3:50 We'll call that variable current user. 3:53 Now, in order to test to test to see if it's Mike, 3:55 we're gonna need to bring in current user, and then do an if statement, and 3:58 then output a string that tells us whether it is or is not Mike. 4:01 So let's do that with starting with a conditional function, so, 4:05 or a conditional if statement. 4:09 So I'll do if, and then we'll say our variable is current_user, so 4:12 current and underscore user. 4:16 Equals, equals to make sure it's equivalent. 4:19 And then, Mike. 4:21 We'll close that with a single quote. 4:22 Close our parentheses and now we have a completed if statement. 4:23 If the current user is equal to Mike, we're gonna wanna output a string. 4:28 So I'll do echo, and I'll say, It is Mike. 4:32 All right. Close the single quote and 4:38 then close the statement with a semicolon. 4:40 So now if current_user is equal to Mike then we'll have a echo statement that 4:42 says, It is Mike. 4:47 Otherwise, so we need an else statement here and inside of our else statement, 4:49 we'll say, it is not Mike. 4:54 So, echo. 4:55 Nope, it is not Mike. 4:57 All right. I'll put a period in here, and 5:03 then a semicolon to close the line. 5:05 Now this is great, except when we call it, so we'll go is_mike. 5:07 The only problem here is that we don't have a variable called, current_user. 5:14 Now, there's something you need to understand about function scope. 5:19 Any kind of variable that's used inside of the function here, 5:22 so inside between lines 10 and line 14, 5:27 anything inside that function is only for that function. 5:32 So, in order to get something from the outside, 5:36 we have to pass that through as an argument, or 5:39 we can use the global statement, which we're gonna do now because we're gonna use 5:42 something outside of the function scope, but we're not into arguments just yet. 5:46 So, let's create this variable, current_user, and 5:50 that current_user variable is gonna be equal to, to start off with, Mike. 5:54 All right, now to get that inside of our function. 6:00 We're gonna need to go inside the function and type global as our keyword, and 6:04 then current_user. 6:09 All right, now that will tell us we have a global variable outside of the function 6:12 scope called current_user that we would like to use inside of our function. 6:16 Okay, now that we've done that, 6:21 let's run this code by going to our preview and see what it looks like. 6:22 So I'm gonna go over to our, preview after I've saved it and then hit Refresh. 6:26 It is Mike, which is correct. 6:32 Now if we changed current_user to say, me, and then saved it, 6:35 and then went back over and refreshed. 6:40 It'll say Nope, it is not Mike. 6:43 So now we have a function that runs, simply from a single line that we 6:44 can find out whether or not the current user is equal to Mike. 6:48
You need to sign up for Treehouse in order to download course files.Sign up