Dynamic websites with PHP (part 1)

Static HTML

So far, all the websites we have built are “static”. In other words, the content we see in the browser window is rendered directly from the HTML markup, using the CSS rules for presentation. It doesn't matter how many times we view such pages, they do not change because the web server is always serving the same html and css files (unless we change them manually).

That's just boring!

Yes, it is. Websites should respond to the user and reflect temporal changes. For example, the copyright date in the page footer should always update to the current year, avoiding the necessity for changing it manually. Webpages should be self-maintaining.

Good news

The good news is that we can do all of this and more using a rather wonderful server-side scripting language called PHP.

Isn't JavaScript our scripting language of choice?

Well, JavaScript is a client-side scripting language, which means it can only act on a webpage once it has been served and downloaded to the local client (browser). Whereas, PHP runs on the server and the instructions we make are executed before the webpage is served. The beauty of this is that we can actually control how the page is built each time it is requested.

Crazy cool!

Sure is. PHP makes the building of even basic websites more efficient and makes them much easier to maintain.

Don't repeat yourself (DRY)

You will have noticed that there is quite a lot of repetition in your static websites. For example, the primary navigation is repeated on every page of your site, so to is the branding and the footer. Maintaining such sites is problematic because every instance of a primary navigation link will need to be modified if the site changes. Wouldn't it be great if the navigation could be changed just once in a single file and the whole site updates automatically? PHP can do this.

Modular websites

We can break our websites down into logical components, put each component into a separate file and then assemble each page as it is requested. We do this using server-side includes and we use PHP to make the whole thing work.

How does the whole “include” thing work?

You are familiar with the concept of linking a CSS file to a HTML file — in effect, the CSS is imported into the head of the HTML document. The same thing happens with includes except that the contents of the included file actually appears as part of the HTML when it reaches the browser. All we need to do is get PHP to tell the server which bits of HTML should be added to which part of the main file.

OK, what does this look like?

Well, if you have a homepage called index.html, the first thing we need to do is change its name to index.php so that the server knows it needs to be dealt with in a particular way (parsed). Once we've done that, we can add PHP to the file, knowing it will be properly processed.

Say we have saved the navigation part of our webpage (just plain html — nothing fancy) in a file called navigation.inc.php, we can include that file using the following bit of php:

<?php include ('navigation.inc.php'); ?>

The include function does all the work. The name of the include file can be anything (stuff.txt would work just as well), but the name I'm using here is designed to tell me what I need to know about its contents and how it should be used.

Yep, it's really that simple…

…well, there are a few more things you need to know before you become an include ninja, but that's the basics of it.