Include files in PHP
This tutorial will teach you how to make updating layout and changing links a lot
easier, using the include(); function.
If you're not familiar with the progamming language PHP, I suggest you to read the Introduction to PHP before you continue.
Let's suppose we have 3 pages, page1.php, page2.php and page3.php. Each of them originally has a header with images, navigation bar and a footer. Header, navigation and footer are the same for all 3 pages, so we'll find a fay how to make updating files easier.
Step 1: Make separate files
Make empty files named page1.php, page2.php, page3.php, top.php,
nav.php, footer.php. Now add content to those files.
top.php - this file should contain header image. For this tutorial, any will do.
Remember, do NOT put any additional HTML
code such as <head>, <body>, <title> etc.
Just the image code!!
nav.php - you should put the main navigation here.
footer.php - in this file write the copyright information and put the bottom image if you want one.
page1.php, page2.php, page3.php - these 3 files should have the same layout,
only different content. Remember, these are actual html files, which means they must
have the basic html structure
(<html><head></head><body></body></html>)
and what comes in between.
Step 2: Connect the files
If you got it all right, now we'll link up the files together. We'll use the function
include(); which does what the name itself says, includes the file inside our
web page. Since we know the names of files to include, we can ommit the brackets
and use only quotation marks. Use this codes - copy them into the files page1.php,
page2.php and page3.php to places provided for top image, navbar and footer.
<?php include 'top.php'; ?>
<?php include 'nav.php'; ?>
<?php include 'footer.php'; ?>
Step 3: Try if it works
If you have Apache and PHP installed on your computer, run Apache and then
open page1.php in your browser.
If you don't, upload all six files to your host and then open page1.php.
You should be able to see the page together with the top image, navbar and footer.
There, you made it :) Now, whenever you change the layout, all you need to do is change the top.php and a bit of CSS. Everything else remains the same. If you want to add more links, you need to change only nav.php. You see how PHP makes updating your website all easier ^_^
