(PHP 4, PHP 5)

La sentencia include_once incluye y evalúa el fichero especificado durante la ejecución del script. Es un comportamiento similar al de la sentencia include, siendo la única diferencia que si el código del fichero ya ha sido incluido, no se volverá a incluir. Como su nombre lo indica, será incluido sólo una vez.

include_once puede ser usado en casos donde el mismo fichero podría ser incluido y evaluado más de una vez durante una ejecución particular de un script, así que en este caso, puede ayudar a evitar problemas como la redefinición de funciones, reasignación de valores de variables, etc.

Ver la documentación de include para información sobre como trabaja esta función.


Con PHP 4, la funcionalidad _once difiere entre sistemas operativos insensibles a mayúsculas y minúsculas (como Windows) así que por ejemplo:

Ejemplo #1 include_once con un SO insensible a mayúsculas y minúsculas en PHP 4

include_once "a.php"// esto incluirá a.php
include_once "A.php"// esto incluirá a.php otra vez! (sólo PHP 4)

Este comportamiento cambió en PHP 5, así que por ejemplo con Windows primero se normaliza la ruta de acceso para que C:\PROGRA~1\A.php sea identificado igual que C:\Program Files\a.php y el fichero sea incluido sólo una vez.

add a note add a note

User Contributed Notes 5 notes

roach dot scott+spam at googlemail dot com
7 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).
xcl_rockman at qq dot com
4 months ago
return array("test">1);

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

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

output will be

flobee at gmail dot com
10 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 {
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:

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

emanuele at rogledi dot com
7 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