Using Variables for the Title Tag3:24 with Alena Holligan
Variables allow you to customize the HTML that PHP produces and dynamically modify it for each page. In this video, we’ll use a variable to give each page a unique value for its title tag.
Update to html
index.php unordered list for our random items should have a class of "items".
As we covered in the php basics course, 0:00 a variable is a piece of code that references some data. 0:02 There are several types of variables. 0:06 Throughout this course, we'll be using strings, integers, 0:09 meaning whole numbers, arrays, and touch briefly on objects. 0:12 The first one we'll be using is a string variable. 0:16 To change the title tag from page to page. 0:19 Let's go back into the suggest.php file. 0:23 Let's start by specifying the page title for the suggest page. 0:26 Right before this line where we include the header, let's add a variable or 0:30 page title. 0:35 As a refresher, variables in PHP are represented by a dollar sign 0:38 followed by the name of the variable. 0:42 Variable names are case sensitive. 0:44 We can make up just about any name we want for our variable. 0:47 A valid variable name starts with a letter or 0:50 an underscore, followed by any number of letters, numbers, or underscores. 0:53 There's no predefined list of variables we have to choose from. 0:58 But it's good practice to make your names descriptive so 1:01 that your code is easier to read. 1:03 Let's assign our variable page title, a string value of suggest a media item. 1:05 If we assign this value to the variable in our code before we include header.php, 1:16 then we can reference this variable in that include file. 1:22 Open header.php. 1:26 Here we have the page title, 1:29 where we've told it to simply echo out the actual title. 1:31 This is not very useful. 1:36 Let's replace the string Personal Media Library, with our variable, page title. 1:38 Be sure to type the variable name exactly. 1:48 Again, upper and lowercase characters do matter. 1:51 Save both files, and let's take a look at suggest in our browser. 1:54 When we refresh the page, notice how the title on the tab has changed. 2:00 Let's take a closer look at what's happening in our code. 2:05 When suggest.php first loads, our code creates a variable named $pageTitle and 2:08 assigns it a value of "Suggest a Media Item". 2:13 Then our code includes header.php. 2:16 Which echos out the value in that page title variable. 2:18 Let's add the same variable with different values to our other pages. 2:22 Open catalog.php. 2:25 And let's add this variable right before I include 2:29 page title equals Full catalog. 2:35 Now lets go into index.php and do the same thing. 2:43 A new variable, page, title. 2:51 And we'll assign this the value of Personal Media Library. 2:56 Now let's go back to our browser. 3:03 As we click through the menu, we can see our title changing as we go. 3:06 By assigning a value to a variable before the header file gets included, 3:13 we can use the same PHP code for all the pages but 3:17 still give them unique values for their title tags. 3:20
You need to sign up for Treehouse in order to download course files.Sign up