php – Including Files in PHP – Beginner Tutorial

First, I want to say this is a beginner tutorial on including files with PHP. However, even if you are an intermediate or slightly beyond user, this tutorial may benefit you in some way because we are going to discuss some security features.

The main purpose of this tutorial is to kick off the new “Beginner” series of PHP tutorials. These tutorials will cover many of the common problems and questions, or misconceptions that we have seen on our forums and throughout the net regarding PHP. This tutorial will be fairly short, so even if you’ve been working with PHP for a while, you may still want to read on.

In addition, this tutorial is not written to read or write to other files. It is simply written to show new users how to include files properly.

The constructs we will discuss in this tutorial should be used when you want to pull together pieces of code or settings for your project. A common scenario would be a group of functions that you use throughout a website, or a class, or even a group of configuration settings, stored in a file such as a config.php with your site’s pertinent information.

The Core PHP Constructs for Including Files
There are four core constructs for including files into your PHP scripts. The main objective is for you to create code in separate files and then be able to use that code to include functions, variables and etc, in other PHP scripts. You have two main options. To include() a file or to require() a file. We’ll get into the specifics in a moment and you’ll quickly understand what the differences are.

The include() Construct

The include() constrcut is the most commonly used method to include files amongst most developers. It’s purpose is to simply include a file and that’s it. If the file does not exist, it will return a warning and still allow the script that’s trying to include the file to continue to operate even if the warning is issued. Here’s a common example:

PHP Example:
<?php
include($_SERVER[‘DOCUMENT_ROOT’].’/myfile.php’);
?>
Now, all of the code, and functions from myfile.php will be available throughout the rest of the current PHP script for use with the rest of your code.

Don’t worry if you do not understand the paths used int he previous example yet, we’ll get into the relationships between the current working directory and the filesystem later in this tutorial.

The include_once() Construct

Ok, the main difference between the include_once() construct and the include() construct is that if the file has already been included in this code execution, it will not be included again. This is a good method to use and I would recommend it above using the standard include() construct because it can prevent you from redeclaring functions that you may have already included previously. As your code becomes more complex, you may have files included in different files and when calling those files, you may start running into problems.

My recommendation: if you need to include a file using one of the include methods, use include_once() as the preference for construct of choice!

PHP Example:
<?php
include_once($_SERVER[‘DOCUMENT_ROOT’].’/myfile.php’);
?>

The require() Construct

The require() construct is the same as include, but one major difference. If the file does not exist, or cannot be included, a Fatal Error will be produced and the execution of the PHP script will be halted! This construct is important for those applications you may develop that have dependancies from other files which must be met in order for your script to function properly.

PHP Example:
<?php
require($_SERVER[‘DOCUMENT_ROOT’].’/myfile.php’);
?>
The require_once() Construct

This construct is the one that I use more than the other three. Personally, I feel that this construct takes into account all of the necessary reasons you would be including a file in the first place. Just like include_once() the require_once() construct determines if the file has already been included and if it has been, it will skip this instance. In addition, a Fatal Error will be produced just like the require() construct does if the file cannot be read or included.

PHP Example:
<?php
require_once($_SERVER[‘DOCUMENT_ROOT’].’/myfile.php’);
?>

1 Comment »

  1. amitmondal said

    Nice tutorial….thanks for this post…..
    Please visit this link for learn more script.
    http://amitmondal.wordpress.com/

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: