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