PHP: include_once - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Функции require_once

Last updated: Sat, 27 Jan 2007

view this page in

# include_once()

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() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.

For more examples on using require_once() and include_once(), look at the PEAR code included in the latest PHP source code distributions.

Return values are the same as with include(). If the file was already included, this function returns `TRUE`

> Замечание: include_once() was added in PHP 4.0.1pl2

> Замечание: Be aware, that the behaviour of include_once() and require_once() may not be what you expect on a non case sensitive operating system (such as Windows).

> Пример 16-12. include_once() is case insensitive on Windows

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

> This behaviour changed in PHP 5 - 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.

Внимание
Версии `PHP` для Windows до PHP 4.3.0 не поддерживают возможность использования удаленных файлов этой функцией даже в том случае, если опция allow_url_fopen включена.

See also include(), require(), require_once(), get_required_files(), get_included_files(), readfile(), and virtual().

add a note User Contributed Notes
include_once
gal
19-Jan-2007 04:58
`the problem is that the included code will be nested in the function you supplied.`
tszming at gmail dot com
16-Oct-2006 07:35
`a simple & optimized way to include once
function my_include_once($path) {
if ( !defined($path) ) {
include($path);
defined($path, 1);
}
}
// sample
my_include_once("/home/www/class/object.php");`
webmaster AT domaene - kempten DOT de
10-Aug-2006 05: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:
<?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`
29-Aug-2005 01: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) {
... }
with
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 http://php.net/function_exists 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 07: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:
<?php
if(!@include_once('./somthing') ) {
echo 'can not include';
}
?>
solution:
<?php
if(!@file_exists('./somthing') ) {
echo 'can not include';
} else {
include('./something');
}
?>`
Pure-PHP
17-Mar-2005 02: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
http://www.pure-php.de/node/19
include_once("includeWrapper.class.php")
includeWrapper::includeOnce("Class1.class.php");
includeWrapper::requireOnce("Class1.class.php");
includeWrapper::includeOnce("Class2.class.php")`
bioster at peri dot csclub dot uwaterloo dot ca
28-Oct-2004 03: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.`
add a note
Функции require_once

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST