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).

Пример #1 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.


Версии PHP для Windows до PHP 4.3.0 не поддерживают возможность использования удаленных файлов этой функцией даже в том случае, если опция allow_url_fopen включена.

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


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') ) {
'can not include';

if(!@file_exists('./somthing') ) {
'can not include';
} else {
2005-05-26 10:55:47
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 (
// ... or any other include[_once] / require[_once]

just adjust the path and it will be fine - also for your IDE.

2006-08-10 08:11:46
For include_once a file in every paths of application we can do simply this

include_once($_SERVER["DOCUMENT_ROOT"] . "mypath/my2ndpath/myfile.php");
2008-05-18 20:40:08
If you include a file that does not exist with include_once, the return result will be false. 

If you try to include that same file again with include_once the return value will be true.

(include_once 'fakefile.ext'); // bool(false)
var_dump(include_once 'fakefile.ext'); // bool(true)

This is because according to php the file was already included once (even though it does not exist).
2008-06-27 20:22:28
return array("test">1);

$config = include_once("config.php");

$config = include_once("config.php");

output will be

2015-03-17 09:56:24
require_once() can check the file if once include ,or the file is wrong will tell a error and quit the script.
2015-08-14 06:57:12
In response to what a user wrote 8 years ago regarding include_once being ran two times in a row on a non-existent file:

Perhaps 8 years ago that was the case, however I have tested in PHP 5.6, and I get this:

$result = include_once 'fakefile.php';  // $result = false
$result = include_once 'fakefile.php'   // $result is still false
2017-01-24 22:16:23
i tried

-------------------------------- index.php: 
$out='todo 1';
include 'file1.php';
$out='todo 2';
include 'file1.php';
echo 'end';
-------------------------------- file1.php:
include_once 'file2.php';
 echo $out;
-------------------------------- file2.php:
$out='first todo once';
include 'file.php';

the output is:
first todo once
first todo once
todo 2

what should i do?

if i would replace in file2.php include with include_once, then i have following output:
first todo once
todo 2

i could write
$out='first todo once';
include 'file1.php'
$out='todo 1';
include 'file1.php';
$out='todo 2';
include 'file1.php';

but i don't want :-)
please help
2017-03-21 15:39:57

    Поддержать сайт на родительском проекте КГБ