PHP Includes
Introduction
include() is probally one of the most commonly used PHP functions, it allows you to display the content of a file within another page with just one simple line.
There's probally many things you could make use of php includes for, the most common being a template based website where you'll have a number of content pages but only one or two pages with the code for the main layout which is then included into the content pages.
There are some slight variations on the include function like require() and virtual() they are all very similar but do have their differences, so once you get used to using include() and understand how it works, perhaps take a look at the PHP Manual to discover what those slight differences are.
The biggest benefit to using includes for atemplate based website is the amout of time it saves when making changes, for example if you want to change the info that you see at the bottom all pages pages, you would only need to make the change to a single file.
Quick Example
Here's something very simple you can try out incase you're still a little confused.
- Add the following to a blank file:
1<br />
2<br />
3<br />
<?php include 'file2.php' ?>
Don't bother with anything else like <html> and <head> tags etc, just copy and paste that into the blank file and save it as "file1.php"
- Add the following to another blank file:
<br /> 5<br /> 6
- Save it as "file2.php"
- Upload both files to your webspace
Now view file1.php from your browser yoursite.com/file1.php etc and you should see a page that looks just like this one
This isn't obviously the greatest example, but hopefully you can see how easy it is to include the entire content of one page into another by using just one line:
<?php include 'filename.php' ?>
Also take a look at the source of that page, you'll see that from a browser it looks just like a normal page none of the scripting is viewable.
note: The page you're using in the include() doesn't have to be a .php page it can be .html .txt almost anything you like, but obviously make sure the page you're calling it from has a .php extension.
Web Template
It doesn't really get alot more complicated than that, you may want to have a look at some of the finer details of the include() function in the php manual (link above) but you know how to use them, you've seen a basic example of how they work so the best thing to do is to experiment, and see how you can make use of includes to save you time and effort in your pages.
If you want to play around a little more and see how it makes things easier with a template based site, here's a very basic site template that uses includes, so you can upload it and mess around a bit. It's probally best to make a directory called "test" and save these files to there, it'll keep everything in one place and you wont risk erasing something you already have.
Copy the code for each of these six files into a blank page and save them as stated:
main.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/XHTML1/DTD/XHTML1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>PHP Includes - Example</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
p { padding:8px }
#logo {
height:75px;
font-size:24px;
margin-bottom:15px }
#left, #right { clear:both }
#left { width:15%;float:left }
#right { width:82%;float:right }
#logo, #left, #right {
background:#eee;
border:1px solid #aaa }
#left ul, li { margin:8px;list-style-type:none }
body { font: normal 12px verdana, tahoma, sans-serif }
</style>
</head>
<body>
<div id="logo">
<p>My Logo</p>
</div>
<div id="left">
<ul>
<li><a href="./index.php">Home</a></li>
<li><a href="./page1.php">Page 1</a></li>
<li><a href="./page2.php">Page 2</a></li>
<li><a href="./page3.php">Page 3</a></li>
</ul>
</div>
<div id="right">
<!-- break for content -->
footer.php
<!-- end of content -->
</div>
</body>
</html>
index.php
<?php include 'main.php' ?>
<p>Put your homepage content here.</p>
<?php include 'footer.php' ?>
page1.php
<?php include 'main.php' ?>
<p>Put your content for page 1 here.</p>
<?php include 'footer.php' ?>
page2.php
<?php include 'main.php' ?>
<p>Put your content for page 2 here.</p>
<?php include 'footer.php' ?>
page3.php
<?php include 'main.php' ?>
<p>Put your content for page 3 here.</p>
<?php include 'footer.php' ?>
Now once they are uploaded go to yoursite.com/test/ and you can see by clicking the links for page 1, 2, 3 and home how includes can be used to build a template and have as many different pages as you like without having to write your entire template code into each. Here's how it should look.


