Take the 2-minute tour ×

This question already has an answer here:

What is the best practice for including PHP files?

Is it best to include a include.php file that includes all of the project PHP files? Or to include it in those files that need them

Right now, my project has several include files in my index.php file. Does including all of my php files in the index.php make it less efficient?

Lastly, Where should one include the session check PHP file? in all of the PHP files?

share|improve this question
To answer your session check question.. Do you use a frontcontroller? Or in other words, are all requests routed through index.php or do you have separate php files for different pages (login.php , register.php) –  TFennis Dec 2 '11 at 10:18
How do you include static assests such as JS and CSS files in html code embedded in a php script? –  Darshan Thanki Jan 12 '13 at 6:10
add comment

marked as duplicate by Dave Jarvis, pduersteler, EdChum, rorra, mattytommo Apr 5 '13 at 8:06


5 Answers

up vote 2 down vote accepted

Usually, I have a config file like this:

define(root, $_SERVER['DOCUMENT_ROOT']);
.... // other variables that are used a lot

include (root . '/class/database.php'); 
.... // other includes that are mostly called from each file, like a db class, or user class, functions etc etc...

if (defined('development'))
    // turn error reporting on
    // turn it off

etc etc... You got the point of config.

And I include the config.php on each file. I forgot how to do it right now, but apache can do the automatic include for you. Therefore, you can say to apache to include your config file by default.

Then, I have controller classes, which call the views. There in each function I call the view.


function index() { include root . '/views/view_index.php'; }

finally, from the view, if I need to include the header and footer view I do it like this:


<?include root . '/view/shared/header.php';?>
<div class="bla bla bla">bla bla bla</div>
<?include root . '/view/shared/footer.php';?>

I always use include in this structure rather than include_once since the latter requires extra check. I mean, since I am pretty sure that I include files only once, I don't need to use include_once. This way, you also know which include is where. For instance, you know that crucial files like db.php, or functions.php are located in config.php. Or you know that include views are located in controllers. That's pretty useful for me, I hope that helps you, too.

share|improve this answer
add comment

Using the include.php file is a very good practice according to me, as it is very helpful in changing the included files in big projects. If the project is small then including individual files is not a problem. But it becomes a problem to manage them as the project grows big.
For the session check file it is better to attach them individually as the requirement to check session on different pages might differ.
Including files individually or including them all in a single file and then including that makes much of the difference to the performance. As ultimately all the files are going to be included. It only becomes easy to manage them if single file is used to handle them.

share|improve this answer
add comment

I don't assume you are using object oriented programming but in case you do here might be a good answer.

In php you can define a function called the autoloader, if you try to create an object of a class that has not been defined the autoloader is called. You can then use the class name to figure out where the file containing that class is stored to include it at the last moment. Here is an example..

function on_load($class)
          throw new Exception('Class not found: '.$class.' in classes/');

spl_autoload_register('on_load'); // tell php to call your on_load function if the class was not defined

If you're working on a big project you might want to group your files like this

/classes/database/PDO.php // I'm just listing random stuff
/classes/Core.php // Whatever

You can then use a special naming convention to find the right directory

class Database_MySQL{} // look in <root_dir>/database/ for MySQL.php
class Core             // look in <root_dir>/ for Core.php
class Database_Driver_Adapter_Useless_MysqlAdapterThingy {} // look in <root_dir>/Database/Driver/... blabla

Or you can use the php 5.3 way and define your classes like this

namespace database\driver\adapter\useless;
use database\driver\adapter\MysqlAdapter; // Now you have to tell PHP which version of MysqlAdapter class you want to use, even if there is just one
class MysqlAdapterThingy extends MysqlAdapter {}

Now you have to use the 'use' keyword in every file you need that class. The cool thing is that the namespace is automatically added to the class-name for your autoload function so you can do something like this

function on_load($class)
{ require_once('root/' . str_replace('\\', '/' $class)); }

If you want to learn more try googeling PHP auto-loading there is tons of information on this subject. But then again. From the format of you question I do not assume you're using OOP so this answer is just for the people who found this question on google.


I would also like to add the following:

 // Only include the file once even if you place this statement multiple times

require('bla.php'); // Error if file doesn't exist, php will not continue
inlcude('bla.php'); // Warning if file doesn't exist, but php will continue
  • Using include or require without _once means that file will get included every time the statement is executed
  • Use include for template or user generated files, use require_once for classes
share|improve this answer
add comment

Just think easy, and think about load as less as possible and don't include something unnecessary.

So for your PHP files. if you include same php files on different pages just create 1 PHP file with this files in it. If you use a PHP file in 1 page or 2 only, just include them seperate.

I hope i helped you with it ;)

share|improve this answer
add comment

Include files independently, use require_once() function instead of include, as require_once allow only one inclusion of file...

share|improve this answer
Why do you think this is an answer of asked question? –  Framework Dec 2 '11 at 9:47
add comment
