Even if you’ve never heard of PHP before, using the includes() function can make your web designing run much more smoothly. I remember way back when I started my very first website. It was a splash page, leading to a list of links that led off to other pages. Not very attractive, but it worked. It made it easy to add new content. Whenever I tacked on another page, I’d just go back to my list of links and add to the bottom.
Then I learned the subtle art of web design. Now my pages all had a uniform look with a standardized list of links on each one. It looked great. Of course, every time I wanted to add a new feature, I had to go in and manually add that link to each and every page. Even with a small site, it took too long. It was slow and tedious and monopolized time I could have spent working on actual content.
For awhile, I switched to frames, keeping my links in one frame with my content in the other. When I wanted to add a new link, I just tacked it into my menu frame and it changed on all the other ones. Of course, frames have their own issues. Visitors can’t bookmark specific ones, and as web design goes, they seem to be an outdated concept.
However, using the PHP includes() function, you can replicate this idea of having one source for the same content on many pages. And better yet, unlike a frame, where you’re stuck with it off in a specific place, includes() can be put anywhere in your page that you’d like. You don’t have to use them just for menus, but any content that you find yourself adding and updating on multiple pages, even the Google analytics tracking code.
So how do you use includes? It’s easy. You’ll first need to create a new file where you’ll put the content you want to replicate on many pages. If, for example, it’s a site menu, put in all the HTML code you would use to make just the menu. This is easiest done as a header or footer. In this example, we’ll say you want to put a strip of links across the top of each page in your site.
Now, a word of warning: You will need to rename your pages to have a .php extension instead of .html for the code to work. You can always redirect people from your existing .html page with META tags.
Once you’ve created your menu code in a separate file, save it as something you’ll remember, like menu.php. Now, back on your original page, anywhere you’d include that code from your file manually, just put in:
That’s really all there is to it. Now when the page loads, the code stops when it hits that function, grabs all the content from menu.php and dumps it into the page right there before it keeps on going. In fact, it does it so transparently that even someone viewing source just sees the menu code in its spot on the page.
Now whenever you need to update your menu code, you just edit the menu.php file and everywhere it’s included is updated automatically. Which means you spend less time digging around in HTML and more time producing content.