Login |  Register 
This has got to be one of the most frequently updated PHP sites, keep up the good work...
Max
 

Including and requiring files in other files

Often you want to create PHP codes in seperate files for different tasks. For instance you might have one file database.php for accessing the database another file users.php for handling users and finally the main web page index.php.

In PHP this files can be included in the main page using include or require functions.

Both of these functions are interchangable in the below examples, with one difference explained below. The functions include the files so that when PHP evaluates the file with the includes it appears to be one file, therefore the functions are all visible to one another, excluding any object orientation issues.

database.php file
<?php
function connect()
{
 //connects to the database
}
?>

users.php file
<?php
function isUser($username)
{
 //checks if a username is an existing user.
}
?>

index.php file
<?php
//include the database file
include('database.php');
//include the users file
include('users.php');

function login($username, $password)
{
 //This function was present in the database.php file
 connect();
 //This function was present in the database.php file
 if (isUser($username))
 {
  //Carry on logging in
 }
}
?>
As you can see above the include functions have included the files in the index.php file allowing the index.php file to use the database.php and users.php functions. Of course any item be it variables, classes or functions etc can be used from the database.php and users.php functions.
require('user2.php');
Here the user2.php file doesn't exist, but because the require function is used it will throw a fatal error as the file requires function indicates that this file needs user2.php file to exectute. However if a include was used it would only return a warning as the file isn't required to execute. Another problem that can occur is that if a file is included in a child file and is included again in a parent file it can cause an error so below we have an example of this occuring.
database.php file
<?php
 /*
  * The rest of the database file
 /*
?>

users.php file
<?php
//include the database file
include('database.php');
 /*
  * The rest of the users file
 /*
 ?>

index.php file
<?php
//include the users file
include('users.php');
//Its included again!!
include('database.php');

/*
 * The rest of the index.php file
 */
?>
PHP will throw an error because the database.php file has been included twice, once in the users.php file and once in the index.php file.

Ensure a file is only included or required once

To ensure a file is only included or required once you need to use the include_once or require_once these files will only include a file once no matter how many times the file is included in the children files. This should always be used if there is a chance a file could be included multiple times. So for example...
database.php file
<?php
 /*
  * The rest of the database file
 /*
?>

users.php file
<?php
//include the database file
include_once('database.php');
 /*
  * The rest of the users file
 /*
 ?>

index.php file
<?php
//include the users file
include_once('users.php');
//Its included again!!
include_once('database.php');

/*
 * The rest of the index.php file
 */
?>
When the index.php file is evaluated it will not throw an error because the database.php file was only included once in the users.php file and the users.php was the only file included once in the index.php. As a rule it is usually always a better idea to use the include_once and require_once functions rather than than the include and require functions.

Comments to date: 3. Page 1 of 1. Average Rating:
hjl   2:17am on Monday, October 24th, 2016 
good
Ahmed Abdallah   9:04am on Saturday, March 21st, 2015 
i think good but you need to make it very clear more than this
Emily   11:09am on Friday, July 15th, 2011 
Please keep thriowng these posts up they help tons.

security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38