require_once()
The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again. See the documentation for require() for more information on how this statement works.
require_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 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
Замечание: require_once() was added in PHP 4.0.1
Замечание: Be aware, that the behaviour of require_once() and include_once() may not be what you expect on a non case sensitive operating system (such as Windows).
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 required just once.Пример #1 require_once() is case insensitive on Windows
<?php
require_once "a.php"; // this will include a.php
require_once "A.php"; // this will include a.php again on Windows! (PHP 4 only)
?>
Версии PHP для Windows до PHP 4.3.0 не поддерживают возможность использования удаленных файлов этой функцией даже в том случае, если опция allow_url_fopen включена.
See also require(), include(), include_once(), get_required_files(), get_included_files(), readfile(), and virtual().
Коментарии
If your code is running on multiple servers with different environments (locations from where your scripts run) the following idea may be useful to you:
a. Do not give absolute path to include files on your server.
b. Dynamically calculate the full path (absolute path)
Hints:
Use a combination of dirname(__FILE__) and subsequent calls to itself until you reach to the home of your '/index.php'. Then, attach this variable (that contains the path) to your included files.
One of my typical example is:
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/config.php');
?>
instead of:
<?php require_once('/var/www/public_html/config.php'); ?>
After this, if you copy paste your codes to another servers, it will still run, without requiring any further re-configurations.
[EDIT BY danbrown AT php DOT net: Contains a typofix (missing ')') provided by 'JoeB' on 09-JUN-2011.]
"require_once" and "require" are language constructs and not functions. Therefore they should be written without "()" brackets!
1 - "require" and "require_once" throw a fatal error if the file is not
existing and stop the script execution
2 - "include" and "include_once" throw a warning and the execution
continues
3 - "require_once" and "include_once" as their names suggests ,
they will not include the file if the file was already included with
"require", "require_once", "include" or "include_once"
try the following code:
create a file called "index.php"
<?php
require "first.php"; // this will include the file
include_once "first.php"; // this will not as it was included using "require"
require_once "first.php"; // this will not as it was included using "require"
?>
and another file that is called "first.php" and write the following header
-------------------------------
<h1>Hello every one</h1>
--------------------------------
i hope this will help you
Be careful when using include_once and require_once for files that return a value:
fiddle2.php
<?php
return "Some String";
fiddle.php
<?php
$s = require_once('fiddle2.php');
echo "\n" . $s;
$s = require_once('fiddle2.php');
echo "\n" . $s;
/* output
Some String
1
*/
The second time require_once occurs, it returns 1 because the file has already been included.