Oh great! Programming time. Please don't be discouraged, the PHP we'll be using is not heavy in the least bit. Plus if I can learn this, anyone can. Trust me.
To begin, we need to save our HTML file as a PHP file so the server knows to parse the file as PHP. Otherwise, it won't work.
Open index.html and save it as index.php in the your same website folder and remove index.html.
Important! If you're computer is not set up as a server and can parse PHP the following will not work. Instead of going through how to set that up just use your hosting provider and ask them if PHP is set-up for use on their servers. To test if either can parse PHP, create a new document named "test.php" and add...
Save it and drop into either your browser or on your hosting server. If your see "Hello" in your browser, you have PHP installed and running.
Back to PHP, the first thing we have to do is identify the parts of the document we want to pass variables to. These are areas that use the same text.
What's nice about this is we have to enter the text once and it populates all the areas we specified. Down the road, if you feel like changing the name of your website you just need to do it once instead of changing it on every page.
We're not going to worry about the main and side menus for now, that will come in the next chapter. Alright, we see that the "Title", "Section" and "Topic" are used multiple times on our page so this is a great opportunity to use some variables. The markup for PHP is very similar to HTML. It requires opening and closing less-than/greater-than symbols. The difference is, the opening less than symbol requires ?php and the closing needs a ? so the server knows what's between them is PHP. Here's what the differences look like.
To start off with some PHP magic, drop in the opening/closing PHP markup right above the opening HTML tag.
Next, PHP variables are specified by a dollar sign ($). If you'd like to learn more about variables, head over here. Say we want to create a variable for the website title. We want to say "This website = Website Title" which would translate to the following in PHP.
The variable "thisWebsite" can be changed to any name you wish as well as the name of your website, just keep it simple. You will also notice a semi-colon (;) at the end of the statement. Just like separating each property with a semi-colon in CSS, PHP uses a semi-colon to separate each statement.
So how do we make use of PHP? Most programming languages use an "echo" statement. Basically, all this does is repeat what is said after the statement. So if you dropped <?php echo "My Name"; ?> into the body of your document, the text My Name would appear on your webpage. What we want to do is "echo" the website name, so let's call that variable in our title tag.
All good right? So since we used our website's title in multiple places, let's add this statement to those areas as well. Add the statement to the h1 tag and the footer.
Easy right? Now if we decide to change the title of our website, we only have to change the text once in the variable and it will populate all the areas we specified. Ah, isn't code great!
Alright, let's do the same for the section and topic. Head up to where we added the website name variable and add...
For the title, drop in...
You can see, each name was separated by a pipe (|). It just makes the title easier to read. Finally, add the topic variable to the <h2> tag.
As I said, don't worry about the menus. We're getting to that now so let's get on to the next section. As usual, Double-check it and download the sample.