My PHP Includes

One of the advantages of having an include is later on, when you have to add or remove a line or 2, you could just do it in the include file and those pages with the include file embeded will be updated instantly.

That saves you the hassle of making changes to each web page.

Once you appreciate the beauty and power of this feature, you'll want to find ways to include as many as possible; including blank include for future use.

Let's look at my template again:

My simple web page template
This is an image file. To copy the actual code go to one-column template

Let's examine the first php include of my template:

Headmeta

This is my first line of php include. It falls in between the opening and closing of the head tag:

<?php include("incl/headmeta.php"); ?>

Without this line, I'll have to copy every line inside the head tag into my web page. It is not much of a trouble since they are already in the template. The trouble comes when I need to edit a line, like inserting an additional line, remove a line, or correct a typo. Imagine I'll have to do it to all my pages. What if have already 20 pages done. With this php include, everything is made easy.

With this include in place, what I need to do is just open this file in /incl/metahead.php

The reason why the head tag is in the HTML file but not here in the include is this. There are times when I need to add in an inline style or a javascript to a page, I could do it at the HTML file before the closing of the head.

Remember at the beginning of each HTML file, there are 2 lines for me to declare the title and description? They will automatically be echoed into the blanks here:

To create a php include file, open your notepad. Copy and paste the codes, named it headmeta.php under the folder /incl. (without the period) You could name it anything you like but it's better the name make sense. Years ago, I started it with include and over the years it had shorten to incl. Recently I renamed it to _incl to keep my files organized.

The HeadMeta php include:

<meta charset="utf-8" />

<meta name="viewport" content="width=device-width; initial-scale=1.0">

<title><?php echo "$title"; ?></title>

<meta name="description" content="<?php echo $description; ?>" />

<!--[if lt IE 9]>

<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

<![endif]-->

<!--[if lt IE 9]>

<script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js">

</script>

<![endif]-->

<link rel="stylesheet" href="../_css/webscom141.css" type="text/css" media="screen" />

<link rel="shortcut icon" href="favicon.ico" />

<link rel="author" href="https://plus.google.com/u/0/104368718424622593356/" />

Additional codes inside the HEAD

The HTML5 script

One day, should I find that the delayed loading was caused by the site offering the script, I could always copy the javascript to my server and call it from my site instead. Should that happened, I'd have to change the line to:

I'll just save the javascript to a folder named /scripts/. That's all.

<script src="http://websdiy.com/scripts/html5.js">

</script>

Additional CSS link

Similiarily, if I have an additional css file, what I need is to add this line before the head tag:

<link rel="stylesheet" href="../_css/webscom141.css" type="text/css" media="screen" />

Summary

  1. Create a sub-directory, name it include or incl as in my case
  2. Copy the php include HeadMeta
  3. Open your notepad, paste the code and save it as headmeta.php inside the include folder
  4. You'll want to change your css file if you name yours differently, like style.css
  5. If you do not want the Google Plus authorship to be included, delete it
  6. If there's any line that you do not want to be included now but may need it in future, comment it but adding <!-- at the beginning of the line and --> at the end of the line
Pages
templates coding
coding my web pages
create my own page template
php include
creation
create web pages 101
building my own web pages
create first landing page
flow
show the right page
build money making website

eskay ng

by eskay ng