1 September 06
Beginner PHP Tutorial 10: Including Files
When a user wishes to construct a large php application, for example a forum or a CMS, the code usually has to be split up. Sure, we can do this with functions, but this is not ideal because all the code is really still together and functions can be expensive (in terms of cpu). A much more ideal method of splitting up the code is to create seperate files. Even better…create seperate folders, each containing php files that do (or have functions/classes that do) a similar job. This means you can split your application up into an organised file structure, much like a filing cabinet. Within this structure it can be easy for you, the programmer, as well as other programmers to easily find what you’re looking for. From experience, this can be extremely handy when debugging a large application and trying to locate an evasive error.
Application Design
Since the action of including files in php is so simple I will also cover application design, which is very relevant and the ultimate reason behind it all. Every php application has some sort of design behind the scenes, in its file structure. Small applications usually run off a small set of files all in the same folder and there is no real need for intensive thinking behind the design. However, as an application becomes larger and larger a design for its file structure becomes more and more neccessary, and the programmer(s) must act sooner rather than later.
Large applications always have varied designs, ranging from good to bad. Very few large applications work from a single file, and this should always be avoided, with almost no exceptions. Better designs utilize a framework or a well known design pattern, depending on the purpose and functionality of the application in question. If an application has a good design to start off, updates and bugfixes are likely to come much faster and in a greater volume. However, if an application doesn’t start with a good design updates become difficult, tiring and error-prone.
How an application should be split
There are a number of ways to split your php application up into folders, but the majority of the php community agree on the seperation of “business logic” and “presentation”. “Business logic” is the code that does the real work of the application, talking to the database and managing the data of the application. The “presentation” is the appearance, mostly containing (x)html and css, although some php code is normally required (or template code, if such a template engine is being used). The php code or template code in the “presentation” should only be to format and display data provided by the “business logic”. While php/template code is allowed in the “presentation”, very little (at best none) html/css should be in the “business logic”.
This basic split contributes alot to application design, but it’s far from the full story. Applications often require library files which contain a variety of functions, some sort of data access layer to allow the application to talk to a variety of different databases, sometimes an interface for multiple languages and an area for settings. All these need to have their own folders, seperate from each other. Unfortunately, even this is far from the full story, but with a simple design extra requirements are easy to add (eg. the requirement for plugins can easily be met by adding a plugins folder). Don’t worry about the code for any of this as of yet, since linking together components of a large application can be quite complex, but try to get the general idea into your head. A later tutorial will get down into the code of such a system.
Including files
Don’t forget what I’ve just taught you, but now revert the idea of the structure back to a simple small application, for example a guestbook. In the guestbook you have 5 files all in the main directory:
- index.php – the main file where you can read all the entries of the guestbook, there is a link to “post.php” for a user to post their own response.
- post.php – this is a form where the user can enter their name and the message which is submitted to “submit.php”, or they can go back to the guestbook using a link back to “index.php”.
- submit.php – this writes the guestbook entry to the database and provides a link back to “index.php”.
- header.php – this is the top html code included in the other pages (except for footer.php) and the pages can include this rather than duplicating code.
- footer.php – similar to “header.php” this is the bottom html code included in every other page (except header.php) to prevent code duplication.
The idea of “header.php” and “footer.php” is to provide a unified look and feel to the application and it also means you only have to change a single file to change all the rest of the files. In a large website or application this can be priceless. For example, if you wanted to change the title you would just change it in header.php and it would change across index.php, post.php and submit.php when you accessed them across the internet.
Firstly, to include a file in php, use the include function. However, since this is a special language construct it is used like so:
include <file>;
with “file” being the name of the php file to be included. So at the top of index.php, post.php and submit.php you would use the following code, to include header.php:
include 'header.php';
This will make php parse header.php before it continues parsing the current file, making any functions defined in that file available for you to use. This works well, however sometimes the same file can accidently be included twice, causinbg a PHP error. To avoid this use the include_once function like so:
include_once 'header.php';
When you use this function php checks to make sure that the file has not already been included before including it. If the file has already been included, this function does not generate a PHP error.
Before ending this tutorial I would like to inform you of the require and require_once functions. On the level we are studying these are exactly identical to include and include_once (require is the same as include, require_once is the same as include_once). However, there is a minor difference between include and require in the error they produce, but this usually makes little difference (if you’re interested take a trip to the php manual).
By using include we can include header.php and footer.php inside index.php, post.php and submit.php, without having duplicate any code.
Summary
I might have skipped through some areas of the tutorial too quickly and if so feel free to use the contact form to ask me any questions about this tutorial. Now you should be able to include one file into another. To test this out create the following two files:
a.php
<?php
include 'b.php';
echo add(2,3);
?>
b.php
<?php
function add($a, $b){
return $a + $b;
}
?>
If you run a.php you should get the output “5”. If you don’t understand this try re-reading the tutorial to improve your understanding.

