Create a website RSS feed

The Include Statement

The include statement

Last update on: 10-10-2008
The include() statement includes and evaluates the specified file, it lets you insert a page or more in a single chosen php page. It is like working with frames in html, for example:

Syntax

include ('page.php');
//or
include 'page.php';
an index.php page with menu:
the html code of index.php
<html>
<head><title>My website title</title>
</head>
<body color="#000000">
<table style="width:750px;margin-left:auto;margin-right:auto">

<tr>
<td style="width:150px; vertical-align:top">
<?php
//we insert the menu page here
include 'menu.php'; 
?> 
</td>
<td style="width:600px;vertical-align:top">

<?php
//we insert the content page here
include 'content.php'; 
?> 
</td>
</tr>
</table> 
Attention: when you insert a page, it should not contain HTML tags as <html> <head> <body> only table and images, or simply the content of the page, otherwise you'll have several times in the html tag Your Page!
Te menu.php page
<table style="width:150px;margin-left:auto; margin-right:auto; ">
<tr>
<td style="text-align:center"><a href="#">Home</a></td>
</tr>
<tr>
<td style="text-align:center"><a href="#">Services</a></td>
</tr>
<tr>
<td style="text-align:center"><a href="#">Contact</a></td>
</tr>
</table>

I will leave you to do the content.php by yourself, do as we did for the menu page.


Now lets do something that will interest you. probably already seen sites like this for example, with page that does not move and only urls that get same change like: http://www.domain.com/?page=news you want to know how. I will explain to you the operation below.
We will use the same index.php page

Lets do some changes to do menu.php page:
<table style="width:150px; margin-left: auto ; margin-right: auto">
<tr>
<td style="text-align:center">
<a href="/?page=news">News</a> </td>
</tr>
<tr>
<td style="text-align:center">
<a href="/?page=guestbook">Guesbook</a> </td>
</tr>
<tr>
<td style="text-align:center">
<a href="/?page=contact">Contact Us</a> </td>
</tr>
</table>

I made the links with an anti-slash (/) but you can replace with index.php?page=news, because behind the (/) it is the index.php that's hided. but I think that you knew it.

Now what our content.php should looks like; here we will need to use conditions:

<?php
//if the variable equal to news
if($_GET['page'] == 'news'){
//we insert news page
include('news.php'); 
}
//else if the variable equal to guestb
elseif( $_GET['page'] == 'guestbook'){
//we insert the guestbooh.php 
include('guestbook.php');
}
//else if the variable equal to contact
elseif( $_GET['page'] == 'contact'){
//we insert the guestbooh.php 
include('contact.php');
}
?> 
Caution: If the called page is in a sub-folder; think to call it with the full path, if for example the news page is in a sub-folder called pages, so the include statement should be like include 'pages/news.php'; .

If you have too many pages, I suggest that you use the switch operator it's so easy to handle as many pages as you want without getting confused with the if and elseif.
This is an example of how to use switch instead of if and elseif

<?php
switch($_GET['page'])
{

case'news':

include('news.php');
break;

case'guesbook':
include('guesbook.php');

break;

case'contact':
include('contact.php');
break;

default:

//Always put a default page that your visitor... 
//will see if the variable is not assigned
include ('home.php');

}
?> 

PHP and MySQL's lessons:

Introduction To PHP
Get Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

Banner HomeServices Contact |  ©2009 http://www.iteachweb.net/