- Worldstart's Tech Tips And Computer Help - http://www.worldstart.com -

Guide to PHP Include

Guide to PHP Include

What is a PHP Include?

If you’re a Webmaster with a multi-page Web site, using a PHP include will probably be helpful for you. This allows you to not only uniform certain parts of your content pages, but update them all by only editing one file.

For example:

Take my blog for instance. (And please excuse the cheap looking arrows. That’s what happens when the new versions of PaintShop do away with shapes and a line tool!) The top screenshot is the main page of my blog and the one below is a subpage. The white table in the middle is where the content goes. That is the only part of the pages that change and everything around it stays the same.

The column on the far left is actually its own file, named “leftside.php.” The same goes for the column on the far right, as well as, the row at the top of the page with the header image. By using a PHP include script, once uploaded to the Web, all your files are merged appropriately to form one page. I can simply use this page over and over, just by changing the name and the content in the middle.

Pretty nifty, huh?! I have at least 13 subpages of content on my Web site. If I had to go in by hand and change each one when I simply want to add a link on the side of the page, I would go crazy. And you probably would too!

So, here’s how you can put a PHP include on your own site.

The top screenshot is the index.php page of my blog, viewed in the Dreamweaver image editor. The split screen view shows you the HTML at the top and what the page would look like without the PHP includes on the bottom. It should look relatively similar using any other editor or even just coding it by hand. This is the same Web page shown in the first image, but without the PHP includes.

Be sure to save your index page, or any page you are using in conjunction with the PHP includes, as a .php file. PHP includes will not work otherwise and you will still be able to edit the page using HTML.

Once you have the basic structure for your index page set up, you’ll want to find the area on your Web page where you want your content to appear. Paste the following code into the HTML area:
<?php include(“FILENAME.php”); ?>

The code can be pasted into tables (as I did in the examples), but it isn’t required.

Change the FILENAME to anything you want. I recommend giving it a name that correlates to the content you’ll be putting in it (for example, “ADS.php” if you’re using it for advertisement for your Web site or “LINKS.php” if you’re adding links to it). This will help you remember it when you’re editing, especially if you have multiple PHP includes on your page.

Next, you’ll want to create a brand new page and save it with the exact same name you assigned to the code above. If the two do not match, the script will not work! Also, this page is where you will put any and all content that you want to appear in that section of your Web page. Example:

Above is a split screenshot of my own leftside.php. As you can see, I used tables to control the width of all the content. If you put text or images that are too wide in a PHP include page, it will only crowd the content on your pages. Other than that, simply code this page normally and be sure to save it in the .php file format.

Once you upload both pages to the Web, they will come together and form one page, as you can see above. Simply copy and paste the same code into the subpages, in order to make them all uniformed.

Viola! You’re done! Now, you’ll never have to update pages individually again!

~ Kimberly Lawson