Simple Variables and Functions10:18 with Jim Hoskins
In this video, we look at how to use variables and functions in PHP to display dynamic content in your pages.
[?Music?] 0:00 [PHP Basics: Simple Variables and Functions with Jim Hoskins] 0:08 In the previous video, we factored out the common header and footer 0:13 into their own separate files. 0:16 Now we're going to take a look at how to use variables and functions to create 0:18 dynamic information inside of those files. 0:21 Now we saw how to use include to factor out the header and the footer of our pages, 0:24 so we don't have to write them into every single one of our content pages. 0:29 Now in our actual site, we actually may need to change certain parts of our header and footer, 0:33 and the way we have it set up now, is an identical header and footer section 0:38 will be displayed on each page. 0:42 But for instance, let's say our page title. 0:44 On each page, we want the title to reflect the actual page that we're on. 0:47 But since the title tag is in our header. php file, we need a way to place our pages information 0:51 into the generic header.php file. 0:59 Well, we can do that using variables. 1:02 Now let's look at our index.php page. 1:04 Now we're going to go ahead and change this to Home. 1:07 Now we want this Home to be included in our title, 1:11 but our title is inside of our header.php file, 1:15 so we can use a variable, and we are going to do it just like this. 1:19 Now I'm going to split our PHP code section on the multiple lines 1:22 because I want to do something before I include the page. 1:27 A variable is a named piece of information that can store a value and be recalled 1:30 over and over again. 1:35 I'm just going to show you. 1:36 Now variable names in PHP start with a dollar sign, so you can easily identify them. 1:38 Then they have a name. 1:43 In our case, we're going to name this variable "title". 1:44 And now we want to actually give it a value. 1:48 So on this page, our title should be Home. 1:50 Now we're going to do that by creating a string, which if you remember, 1:53 is done by placing quotes, and we'll go ahead and say "Home" and place a semicolon 1:56 because semicolons come at the end of each line. 2:03 So we've said, here is, our variable name $title will hold the value "Home". 2:05 Now that's good, but we actually want to use this. 2:10 So let's use it on this page and replace this hardcoded Home 2:13 with the reference to the title variable. 2:16 So we're going to take this, and we're going to replace this with a PHP code block, 2:19 and inside of this, we want to take the value out of our title variable 2:24 and place it into the output content of the page. 2:28 We do that using what we call echo, 2:31 and just like include, echo is a special language construct and not a function, 2:34 so this also does not have parenthesis like a function would. 2:39 So we say, echo and whatever we place after the statement will be placed into our page. 2:44 So in this case, we are going to place a variable--our title. 2:48 So when we save it out, and we go to our page, and we go Home, 2:54 we can see that Home is printed out here, 3:00 and just to show you, if we change this string right here, 3:03 it will change the value out here. 3:07 Echo is a powerful command that allows us to print contents from a PHP block 3:10 into the actual output of our page. 3:16 Notice I left a semicolon off there, and I want to place a semicolon in there 3:18 because it's a good habit even though it may not be necessary in this particular case. 3:21 Now we actually want to place it into the title tag of our header.php file. 3:26 So we're going to open up our header.php and scroll down to the title tag, 3:30 which we have here, and let's go ahead and place the same thing. 3:36 Now we're actually going to use the exact same code, 3:42 so we're going to do PHP, and we're going to say echo and we're going to say $title, 3:44 and let's see if that worked. 3:49 So we'll flip to the page and refresh, and we can see that our title now displays 3:52 the same thing as this title right here. 3:57 Now how does this work? 4:01 Well in index.php, we created a variable called title, 4:02 and then we included this header.php file. 4:07 When we include this header.php file, all of the code inside of header.php 4:10 has accessed all the variables in all of the state of the file that included it. 4:16 Now if we were to just run header.php without having included it from another page 4:21 that defined title, this would actually be a blank value and we wouldn't see anything. 4:26 So this is a powerful way for us to pass information to a more generic page 4:30 to make something more specific. 4:36 So you see that our title is accessible both from within the exact same source file 4:38 as well as any source file that is included. 4:42 So we can do the same thing on our about.php. 4:46 We are going to want to go ahead and create a title variable of "About us"; 4:49 and let's just move that to the next line, 4:59 and here we can replace this with echo $title; again. 5:02 And so if we go to our About page, we see About us is down here and up here. 5:08 Now you'll notice what we just did is actually have to go and change things 5:12 on multiple lines. 5:17 Now just because we factored a lot of code out by including header.php doesn't mean 5:19 that there is not things repeated. 5:24 In fact, this include on both sides of header and footer are repeated code. 5:27 Now there is some other patterns we can use to get around this, 5:32 but at some point there's going to be some repeated code. 5:34 And that's why you want to make sure to think about these things before 5:38 you actually implement them. 5:41 For instance, if I have a couple dozen .php files, all with include "_header.php" 5:43 and now we wanted to change the name of header.php 5:51 to some other file, I would still have to go and change all those different files. 5:53 So we want to think about that in advance, and try to have as little duplicated code as you can 5:58 to allow yourself to make changes without having to change a bunch 6:04 of different source files. 6:07 We'll look at other ways that we could structure this sort of site that can minimize 6:09 this type of hassle. 6:13 Now let's go into our footer.php. 6:15 Here we see our copyright--year of 2010. 6:18 Let's say below this we wanted to place the current time when the page was loaded. 6:22 Well let's create a PHP tag, and we're going to want to echo something, 6:26 and now we actually want to get the current time. 6:31 Well, we're going to want to use a function that's included in PHP to do this. 6:34 The php.net website is invaluable to use while you're using PHP. 6:37 This has a listing of all the built-in functions as well as some great examples on how 6:42 to use them. 6:46 Now I know just from my experience that to get the date or time or something, 6:48 I want to use the date function, so I'm going to type in date into the search, 6:52 and it's going to take me to the date function page of the php.net website. 6:57 So here we can see that it's in both PHP 4 and 5, 7:01 We're using PHP reverend 5, so we know that it's good, 7:05 and it's going to give us a description and see that it returns a string, 7:08 and it takes in a format value as well as an optional time stamp. 7:12 When you see an argument here enclosed in square brackets, 7:17 that means that it's an optional argument that you don't have to pass. 7:20 Now it will give you some more information on each of the parameters in this box down here, 7:24 and this is good because the way a date works is, it'll print out the current time and date 7:27 in a special format based on the special string that we pass as an argument, 7:33 and inside of that string, we use special characters to say whether we want the date 7:38 or the seconds, or the hour or whatever part of the date or time we want in what format. 7:42 So this table is really invaluable. 7:48 So let's use their example format here. We're going to copy this. 7:51 And we'll go back to our code, 7:56 and we're going to say echo date, 7:58 and our string format will be this, and let's see what this prints out. 8:00 So let's go back to our About Us page, 8:06 and we see it says Wed, 21st of July, a shortened July, 2010, 17, 21, and 29 seconds. 8:10 So looking at this we can see that this is the shortened date, and this is the day, 8:20 the month, the year, the hour, the minute, and the second. 8:27 The colons don't have any special value. 8:31 They're literal values which we saw in the format here. 8:33 So let me go ahead and just place a line break here so we can see that a little easier, 8:37 and now, in our copyright, let's say it rolls over to 2011, 8:44 and we want to update all of our pages. 8:48 Well, we could do that very easily since it's in the bottom of our footer.php file. 8:51 We only have to do it once, 8:55 but let's say we want it to do it automatically so we don't have to worry about it on January 1st. 8:56 Well, we can replace this 2010 here, and use the date function to print out the current year. 9:02 So let's create a PHP tag right in line with our code. 9:07 We're going to say echo, and we're going to use the date function, 9:11 and we say date, put our parenthesis, and then we have to pass at a format string, 9:15 so just like we did before, we'll create a string, and I just want to use the year, 9:21 which is a capital Y, and we can see that in the documentation here. 9:25 The capital Y is a 4-digit date. 9:30 If we wanted to do a 2-digit date like '99 or '03, a lower case y is what you would use. 9:34 This table really comes in handy because almost nobody can memorize all of these 9:40 different formats. 9:43 So we're just going to put a Y here, save it out, and if we go back to our page, 9:46 and refresh, we can see it's 2010. 9:51 Now I can't go forward in time to show you what it would look like in 2011, 9:54 but trust me, when our system time rolls over the year 2011, 9:58 this will automatically change to display 2011. 10:02 Just like this string changes to show the current time. 10:05 In the next video, we're going to take a look at how to handle forms in PHP. 10:09 [?Music?] 10:13
You need to sign up for Treehouse in order to download course files.Sign up