The include_once() 语句在脚本执行期间包含并运行指定文件。此行为和 include() 语句类似,唯一区别是如果该文件中的代码已经被包含了,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。

include_once() 应该用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。

使用 require_once()include_once() 的更多例子见最新的 PHP 源程序发行包中的 PEAR 代码。

返回值和 include() 相同。如果文件已被包含,本函数返回 TRUE

注: include_once() 是 PHP 4.0.1pl2 中新加入的。

注: 要注意 include_once()require_once() 在大小写不敏感的操作系统中(例如 Windows)的行为可能不是所期望的。

例子 16-13. include_once() 在 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)
此行为在 PHP 5 中改了,路径先被规格化,因此 C:\PROGRA~1\A.phpC:\Program Files\a.php 的实现一样,文件只会被包含一次。


Windows 版本的 PHP 在 4.3.0 版之前不支持本函数的远程文件访问,即使 allow_url_fopen 选项已被激活。

参见 include()require()require_once()get_required_files()get_included_files()readfile()virtual()

add a note add a note User Contributed Notes
tszming at gmail dot com
17-Oct-2006 10:35
a simple & optimized way to include once

function my_include_once($path) {
   if ( !defined($path) ) {
       defined($path, 1);


// sample

webmaster AT domaene - kempten DOT de
10-Aug-2006 08:11
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.

29-Aug-2005 04:52
Dealing with function redefinitions

include_once and require_once are very useful if you have a library of common functions.  If you try to override with - that is define - an identically named local function however, PHP will halt noting that it cannot redeclare functions.  You can allow for this by bracketing (within the include file):
function myUsefulFunc($arg1, $arg2) {
     ... }


if (!function_exists('myUsefulFunc')) {
function myUsefulFunc($arg1, $arg2) {
     ... }}

Top level functions (ie. those not defined within other functions or dependent on code running) in the local file are always parsed first, so within the included/required file is safe - it doesn't matter where the include statements are in the local code.

Csaba Gabor from Vienna
flobee at gmail dot com
26-May-2005 10:55
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 {
18-Mar-2005 06:17
Inlude_once can slower your app, if you include to many files.

You cann use this wrapper class, it is faster than include_once


bioster at peri dot csclub dot uwaterloo dot ca
29-Oct-2004 06:06
Something to be wary of:  When you use include_once and the data that you include falls out of scope, if you use include_once again later it will not include despite the fact that what you included is no longer available.

So you should be wary of using include_once inside functions.