Using PHP includes to tidy up code

I was writing out an explanation to a friend about how to install and use the website I built for him ((to replace his version of the site)) and in doing it I related my method for keeping my code “clean.” In a sense.

I use a simple PHP function called an include:

<?php include("document.html"); ?>

Where document.html is the piece of html (saved in a seperate file, clearly) that is being included. I usually break each site I develop into the same four or five areas:

Where document.html is the piece of HTML (saved in a seperate file, clearly) that is being included. I usually break each site I develop into the same four or five areas:

  • Header
  • Menu
  • Content
  • Sidebar
  • Footer

The sidebar is the wildcard, sometimes it’s in, sometimes it isn’t. I’ll go ahead and lay down the structure of the site at that point – what will each area basically contain. Then I start cutting a pasting them into new text files, each one labeled appropriately (as listed above). After replacing the html “chunks” with their appropriate include designations I’ll end up with something similar to this:

<?php include("header.html"); ?>
<?php include("menu.html"); ?>
<?php include("content.html"); ?>
<?php include("sidebar.html"); ?>
<?php include("footer.html"); ?>

Now, if I haven’t already I’ll be uploading these to a safe place on my server to test them out. Assuming I punched everything in right the first time, everything should work out fine.

  1. If the menu is actually an HTML file called “menu.html,” and it is being linked to with the PHP include, changing this one file will affect the menu area of every page. This goes to follow with the header, sidebar, footer, etc. Using a little bit of PHP cuts out what could be hours of hassle down the road.
  2. Editing different sections of your website will now become much cleaner. If you were careful about how you built the pages and linked them together, you could open up “content.html” to change the phrasing of a particular sentence on your index page and only be greeted by the simple text ((and basic HTML)). While it may not positively affect your site per se, your view won’t be cluttered any more by strands and gobs of code that you don’t need to be looking at. For me, at least, that is a big plus.

Last I’ll offer a couple tips to working with these include functions. First of all, if you’re going to be creating a different content.html for each page of your website (now in PHP format), create a new folder for it and call it contents. It will only save you headache in the end – imagine forty pages into the future, and having to discern between site_history_02_06.php and its HTML counterpart. Sure, not life threatening, but why not be as neat as possible?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s