Last 5.3 release ever available: PHP 5.3.29 - 5.3 now EOL

include_once

(PHP 4, PHP 5)

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 may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

See the include documentation for information about how this function works.

Note:

With PHP 4, _once functionality differs with case-insensitive operating systems (like Windows) so for example:

Example #1 include_once with a case insensitive OS in PHP 4

<?php
include_once "a.php"// this will include a.php
include_once "A.php"// this will include a.php again! (PHP 4 only)
?>

This behaviour changed in PHP 5, so for example with Windows 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.

add a note add a note

User Contributed Notes 5 notes

up
36
roach dot scott+spam at googlemail dot com
6 years ago
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.

Example:
<?php
var_dump
(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).
up
0
emanuele at rogledi dot com
6 years ago
For include_once a file in every paths of application we can do simply this

include_once($_SERVER["DOCUMENT_ROOT"] . "mypath/my2ndpath/myfile.php");
up
-3
flobee at gmail dot com
9 years ago
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:
<?php
if(!@include_once('./somthing') ) {
    echo
'can not include';
}
?>

solution:
<?php
if(!@file_exists('./somthing') ) {
    echo
'can not include';
} else {
   include(
'./something');
}
?>
up
-11
webmaster AT domaene - kempten DOT de
8 years ago
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:

<?php
// Change to your path

if(strpos(__FILE__,'/lib/') != FALSE){
   
chdir("..");
}
include_once (
'./lib/other_lib.inc');
// ... or any other include[_once] / require[_once]
?>

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

greetings
up
-24
Kamil Pawelkiewicz
1 year ago
PHP can distinguish if file is a symlink while including.
I've created example.php file containing code below:

<?php
 
print __FILE__.' included';
?>

Then I've created symlink to example.php in the same directory.
Executing this code in the working directory:

<?php
 
include_once 'example.php';
  include_once
'symlink';
?>

prints only 'example.php included' instead of 2 lines.
To Top