CakeFest 2017 NYC, the Official CakePHP Conference


(PHP 4, PHP 5, PHP 7)

La structure include_once inclut et évalue le fichier spécifié durant l'exécution du script. Le comportement est similaire à include, mais la différence est que si le code a déjà été inclus, il ne le sera pas une seconde fois, et include_once retourne TRUE. Comme son nom l'indique, le fichier sera inclut une seule fois.

La structure include_once est utilisée de préférence lorsque le fichier va être inclus ou évalué plusieurs fois dans un script, ou bien lorsque vous voulez être sûr qu'il ne sera inclus qu'une seule fois, pour éviter des redéfinitions de fonctions ou de classes.

Voyez la structure include pour plus de détails sur son fonctionnement.


En PHP 4, la fonctionnalité _once diffère avec les systèmes insensibles à la casse (comme Windows).

Exemple #1 include_once avec un OS insensible à la casse en PHP 4

include_once "a.php"// ceci inclut le fichier a.php
include_once "A.php"// ceci inclut encore le fichier a.php! (uniquement en PHP 4)

Ce comportement a changé en PHP 5 : le chemin est normalisé d'abord, donc, le fichier C:\PROGRA~1\A.php est reconnu comme étant identique au fichier C:\Program Files\a.php et le fichier ne sera inclus qu'une seule fois.

add a note add a note

User Contributed Notes 7 notes

roach dot scott+spam at googlemail dot com
8 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.

(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).
Greg McCarthy
29 days ago
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
xcl_rockman at qq dot com
1 year ago
return array("test">1);

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

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

output will be

webmaster AT domaene - kempten DOT de
10 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:

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

1083706899 at qq dot com
1 year ago
require_once() can check the file if once include ,or the file is wrong will tell a error and quit the script.
flobee at gmail dot com
11 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:
if(!@include_once('./somthing') ) {
'can not include';

if(!@file_exists('./somthing') ) {
'can not include';
} else {
emanuele at rogledi dot com
8 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");
To Top