PHP
PHP introduction
PHP basics
PHP variables
PHP functions
PHP conditions
PHP loops
PHP arrays
PHP OOP
PHP strings
PHP forms
PHP entitites
PHP files
PHP include files
PHP date and time
PHP cookies
PHP databases
PHP sessions
PHP summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
VBScript
AJAX

Server scripting

ASP

Making money online

Make money online

PHP include files

When you see a webpage, it seems that you are looking at a single file. This is only sometimes the case, and sometimes it is not. With the use of PHP's file inclusion functionality you can display a page that actually consists of several pages.

This lesson focuses on:

The purpose of including files

Including files can be very useful if all the pages (or many of them) on your website consist of the same parts such as the same header or footer. For example, if you have 30 pages that all have the same header and you need to change something in that header. If you have that header as part of the page in an include file, you can simply change that file and all the pages are updated automatically as opposed to updating each page manually.

The include() function

The include() function is used to include a file on a webpage.

Syntax:

include("fileToInclude");

Example:

<html>
<head>
<title>Include file</title>
</head>
<body>
<?php
include ("header.php");
?>
<h2>Welcome to our site!</h2>
</body>
</html>

In the above example, the code indicates to include the file header.php at the top of the page.

The code from header.php:

<a href="/">Home</a> | 
<a href="about.php">About Us</a> | 
<a href="faq.php">FAQ | 
<a href="contact.php">Contact Us</a>

So the page will display the above code at the top of it and right after that the message "Welcome to our site!" in a level 2 heading.

Output of the above example:

Home | About Us | FAQ | Contact Us

Welcome to our site!

NOTE: Since PHP executes on the server-side, if the user were to look at the source code of a page that has PHP include files, they would only see client-side code and therefore would be under the impression that the page consists of one group of code. In the case of the above example, the developer of the site working on the code would see this:

<html>
<head>
<title>Include file</title>
</head>
<body>
<?php
include ("header.php");
?>
<h2>Welcome to our site!</h2>
</body>
</html>

while a visitor who tries to view the source code of the page would see this:

<html>
<head>
<title>Include file</title>
</head>
<body>
<a href="/">Home</a> | <a href="about.php">About Us</a> | <a href="faq.php">FAQ | <a href="contact.php">Contact Us</a>
<h2>Welcome to our site!</h2>
</body>
</html>

The require() function

The require() function is very similar to the include() function with one major difference -- with the include() function, if a file that you try to include is not found, the rest of the script will be executed anyway, while with the require() function, if a file that you try to include is not found, the rest of the script will not be executed. The require() function requires files.

Example:

<html>
<head>
<title>Include file</title>
</head>
<body>
<?php
//make an error in the file name
//so that it is not found
include ("headerr.php");
print "File not found but this text will be
printed anyway if you use an include() function, 
not if you use a require() function";
?>
</body>
</html>

Output:

Warning: main(headerr.php): failed to open stream: No such file or directory in /home/site/folder/ on line 9 Warning: main(): Failed opening 'headerr.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/site/folder/ on line 9

File not found but this text will be printed anyway if you use an include() function, not if you use a require() function

In the above example, the rest of the script was executed even though the file headerr.php was not found.

Using the require() function, things would be different:

<html>
<head>
<title>Include file</title>
</head>
<body>
<?php
//make an error in the file name
//so that it is not found
require ("headerr.php");
print "File not found but this text will be
printed anyway if you use an include() function, 
not if you use a require() function";
?>
</body>
</html>

Output:

Warning: main(headerr.php): failed to open stream: No such file or directory in /home/site/folder/ on line 9 Warning: main(): Failed opening 'headerr.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/site/folder/ on line 9

In the above example, the rest of the script was not executed because the file headerr.php was not found.

Is it better to use include() or require()?

It is better to use require() because scripts should not be executing if necessary files are missing.

The require_once() function

A third function used to include files is the require_once() function. The require_once() function behaves the same way as the require() function with one difference -- the require_once() function allows the contents of a file to be included only once.

What happens if you try to include the same file twice or more using the require_once() function?

The data from the file will be displayed only once.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments