PHP Velho Oeste 2024

include_once

include_once문은 스크립트 수행기간동안 특정파일을 인클루드하고 적용시킨다. 이것은 include문과 비슷하게 동작한다. 단지 파일의 특정 코드가 이미 인클루되었다면 그 코드는 다시는 인클루드 될수 없다는 차이점만 있다. 이 이름이 제시하듯이 한번만 인클루드할것이다.

include_once는 특정 스크립트 수행기간동안 동일한 파일이 한번 이상 인클루드되고 적용될지도 모르는 상황에서 사용해야 할것이다. 그리고 함수 중복정의, 변수값 중복 지정 등의 문제를 회피하려면 정확히 한번만 인클루드할 때가 있을것이다.

require_onceinclude_once의 더 많은 사용예는, 최신 PHP 소스 내의 » PEAR코드를 참고할것.

반환값은 include와 동일합니다. 파일이 이미 포함되었으면, TRUE를 반환합니다.

Note:

include_once는 PHP 4.0.1에서 추가됨

Note:

include_oncerequire_once는 대소문자를 구별하지 않는 운영체제(윈도우 등)에서 기대하지 않은 동작을 할 수 있습니다.

Example #1 include_once는 윈도우에서 대소문자를 구분하지 못합니다.

<?php
include_once "a.php"// a.php를 포함합니다.
include_once "A.php"// 윈도우에서 또다시 a.php를 포함합니다. (PHP 4만)
?>
이 동작은 PHP 5에서 바뀌었습니다 - 경로를 표준화를 먼저 하기 때문에, C:\PROGRA~1\A.phpC:\Program Files\a.php로 인식하고, 파일을 한 번만 포함합니다.

Warning

PHP 4.3.0 이전의 윈도우 버전 PHP에서는 이 함수를 이용하여 원격 파일에 접근할 수 없습니다. allow_url_fopen을 활성화하여도 마찬가지입니다.

참고: include, require, require_once, get_required_files(), get_included_files(), readfile(), virtual().

add a note add a note

User Contributed Notes 10 notes

up
139
Greg McCarthy
7 years 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
up
176
roach dot scott+spam at googlemail dot com
15 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
3
Ben F
4 years ago
This currently (running latest PHP 7.4 NTS as of this writing, on Windows) returns as expected... false no matter how many times you include an inaccessible file, true if you include it more than once.
up
2
mho
4 years ago
once i wrote script to include multiple files at once from a given location:

function include_load($path)
    {
    foreach(glob($path . "/*.php") as $file)
        include_once($file);
    }

last week i modified my code and merged all include files into one single file. this results in five times faster code. i have to mention, i am talking about +100 files to include where each file includes single function and filename is equal to function-name.
up
-34
xcl_rockman at qq dot com
9 years ago
config.php
<?php
return array("test">1);

-------------
//first
$config = include_once("config.php");
var_dump($config);

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

-------------------
output will be
array(
   
"test"=>1,
)

nothing
up
-27
sascha dot diebel at gmail dot com
7 years ago
i tried

-------------------------------- index.php:
$out='todo 1';
include 'file1.php';
$out='todo 2';
include 'file1.php';
echo 'end';
-------------------------------- file1.php:
include_once 'file2.php';
if(isset($out)){
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
up
-53
flobee at gmail dot com
18 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
-59
webmaster AT domaene - kempten DOT de
17 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
-46
1083706899 at qq dot com
8 years ago
require_once() can check the file if once include ,or the file is wrong will tell a error and quit the script.
up
-77
emanuele at rogledi dot com
15 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