This is an advanced tutorial about the Include function.
This is really useful if you want to create a load of web pages and have data on them that is subject to change.
For example, if I own a company and I have about 200 pages for my website, each page has my contact email. If my email changed and I needed to change that detail on my site, I would have to edit 200 pages.
Ready?
Step 1
Open your favorite HTML editor and copy this code into it;
Code:
<?php
$company_name = "Oosbandz";
$contact_email = "contact@oosbandz.com";
$conact_name = "Oosband McOosbandz";
$url = "http://www.oosbandz.com";
$copyright = "Oosbandz 2006, All Rights Reserved";
$phone_number = "0800 666666";
$address_1 = "00 Oosbandz Road";
$address_2 = "Oosbandz, UK";
$header = "header1.gif";
$css = "style4.css";
$stats = "stats.php";
?>
Obviously all that data is made up XD
Save that file as data.php
---
Step 2
Open up any pages you want to include any of the data above on.
Add this code to the top of the page;
Code:
<?php include("path/to/data.php"); ?> Use the directory folder and don't hyperlink.
Example:
(/files/data.php") - Right
(http://www.oosbandz.com/files/data.php") - Wrong
To show the data on your pages add this code to where ever you want it to appear on the web page;
Code:
<?php echo $phone_number; ?>
That will diplay the data in the "Phone Number" tag.
This method will save you so much time in updating pages, now you only need to update one file at a time.
---
Step 2.5
You may have noticed I added 3 files to the bottom of data.php.
These don't have to be included but I will show you why I put them there.
If I wanted my pages to have different CSS style sheets, I could place a load of .css files in the same directory as the data.php files, and just load which ever one I wanted by using the same code:
Code:
<?php echo $css; ?>
That will load the css file I specified.
This same method can be used for practicly anything. Think of all the fun you could have, you can resize images, create different tables, different header files.
---
Notes
You connot have spaces in the $header (example) tags, instead just use an underscore "_".
The $header (example) tags are case sensitive, meaning $header and $Header would be different items.
Instead of using
Code:
<?php echo $example; ?>
you can use
. It does exactly the same thing, just with less characters.