Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Language Reference : Control Structures : include_once


The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

include_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.

For more examples on using require_once() and include_once(), look at the » PEAR code included in the latest PHP source code distributions.

Return values are the same as with include(). If the file was already included, this function returns TRUE


include_once() was added in PHP 4.0.1


Be aware, that the behaviour of include_once() and require_once() may not be what you expect on a non case sensitive operating system (such as Windows).

Example 7.12. include_once() is case insensitive on Windows

include_once "a.php"; // this will include a.php
include_once "A.php"; // this will include a.php again on Windows! (PHP 4 only)

This behaviour changed in PHP 5 - the path is normalized first so that C:\PROGRA~1\A.php is realized the same as C:\Program Files\a.php and the file is included just once.


Windows versions of PHP prior to PHP 4.3.0 do not support accessing remote files via this function, even if allow_url_fopen is enabled.

See also include(), require(), require_once(), get_required_files(), get_included_files(), readfile(), and virtual().

Related Examples ( Source code ) » include_once

Code Examples / Notes » include_once


Something to be wary of:  When you use include_once and the data that you include falls out of scope, if you use include_once again later it will not include despite the fact that what you included is no longer available.
So you should be wary of using include_once inside functions.


Since I like to reuse a lot of code it came handy to me to begin some sort of library that I stored in a subdir
e.g. "lib"
The only thing that bothered me for some time was that although everything worked all IDEs reported during editing
these useless warnings "file not found" when library files included other library files, since my path were given all relative to the corresponding document-root.
Here is a short workaround that makes that gone:
// Change to your path
if(strpos(__FILE__,'/lib/') != FALSE){
include_once ('./lib/');
// ... or any other include[_once] / require[_once]
just adjust the path and it will be fine - also for your IDE.


Inlude_once can slower your app, if you include to many files.
You cann use this wrapper class, it is faster than include_once


If you are like me and make heavy use of the __autoload magic function, always set include paths so you can just instantiate your class, and have multiple locations and name schemes for your custom libraries then you might be frustrated by simple parse errors being supressed when using @include_once('lib.php').
The solution I came up with was:
function __autoload($class){
 $paths = array();
 $paths[] = "{$class}_lib.php";
 $paths[] = "{$class}_inc.php";
 $paths[] = "{$class}.php";
   foreach($paths as &$path){
     if((@include_once $path) !== false){ return; }//if
   // we are not in a production environment so we want to see all errors...
   $include_paths = explode(PATH_SEPARATOR,get_include_path());
   foreach($include_paths as $include_path){
     // go through each of the different class names...
     foreach($paths as $path){
// attach each class name to the include path...
       $include_file = $include_path.$path;
         if((include_once $include_file) !== false){ return; }//if
 trigger_error("{$class} was not found",E_USER_ERROR);
Now, just make sure you define IN_PRODUCTION_ENV to true or false to get either the slower (with all parse errors shown) or the faster (just suppress everything) autoloading. Hope this helps someone else since it was annoying just having blank screens show up when I had a simple parse error. Thanks to flobee at gmail dot com for providing me with the epiphany on why pages were showing up blank...-Metagg


i already had a discussion with several people about "not shown errors"
error reporting and all others in php.ini set to: "show errors" to find problems:
the answer i finally found:
if you have an "@include..." instead of "include..." or "require..('somthing') in any place in your code
all following errors are not shown too!!!
so, this is actually a bad idea when developing because paser errors will be droped too:
if(!@include_once('./somthing') ) {
   echo 'can not include';
if(!@file_exists('./somthing') ) {
   echo 'can not include';
} else {

29-aug-2005 09:52

Dealing with function redefinitions
include_once and require_once are very useful if you have a library of common functions.  If you try to override with - that is define - an identically named local function however, PHP will halt noting that it cannot redeclare functions.  You can allow for this by bracketing (within the include file):
function myUsefulFunc($arg1, $arg2) {
    ... }
if (!function_exists('myUsefulFunc')) {
function myUsefulFunc($arg1, $arg2) {
    ... }}
Top level functions (ie. those not defined within other functions or dependent on code running) in the local file are always parsed first, so within the included/required file is safe - it doesn't matter where the include statements are in the local code.
Csaba Gabor from Vienna

Change Language

Follow Navioo On Twitter
Alternative syntax for control structures
eXTReMe Tracker