search for in the  
<includeinclude_once>
Last updated: Thu, 19 May 2005

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

Note: require_once() was added in PHP 4.0.1pl2

Note: 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).

Example 16-12. 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)
?>
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.

Warning

Windows versions of PHP prior to PHP 4.3.0 do not support accessing remote files via this function, even if allow_url_fopen is enabled.

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



User Contributed Notes
require_once
miqrogroove
01-May-2005 02:10
require_once() is NOT independent of require().  Therefore, the following code will work as expected:

echo.php
<?php
echo "Hello";
?>

test.php
<?php
require('echo.php');
require_once(
'echo.php');
?>

test.php outputs: "Hello".

Enjoy,
-- Miqro
ulderico at maber dot com dot br
22-Mar-2005 08:38
With both of your functions guys, Pure-PHP and jtaal at eljakim dot nl, you'll not have any variables available GLOBALly if they're supposed to be globals...

That's why my import handles better those situation. OK, SOME MAY DISPUTE that using include_once and require_once may slow down an application. But what's the use to do IN PHP what the interpreter *should* do better for you. Thusly these workarounds shall, some time in the future, DIE.

Thus It's better to well design your application to keep some order using few INCLUDES and REQUIRES in it rather than insert MANY AND SEVERAL *_once around.
Pure-PHP
17-Mar-2005 05:19
require_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

require_once("includeWrapper.class.php")

includeWrapper::require_once("Class1.class.php");
includeWrapper::require_once("Class1.class.php");
includeWrapper::require_once("Class2.class.php")
jtaal at eljakim dot nl
10-Mar-2005 08:00
When you feel the need for a require_once_wildcard function, here's the solution:

<?php // /var/www/app/system/include.inc.php

function require_once_wildcard($wildcard, $__FILE__) {
 
preg_match("/^(.+)\/[^\/]+$/", $__FILE__, $matches);
 
$ls = `ls $matches[1]/$wildcard`;
 
$ls = explode("\n", $ls);
 
array_pop($ls); // remove empty line ls always prints
 
foreach ($ls as $inc) {
   require_once(
$inc);
  }
}

?>

The $__FILE__ variable should be filled with the special PHP construct __FILE__:
<?php // /var/www/app/classes.inc.php

require_once('system/include.inc.php');
require_once_wildcard("classes/*.inc.php", __FILE__);

?>

The (*.inc.php) files inside the directory classes are automagically included using require_once_wildcard.

This solution may not be as useful when using PHP5 in combination with classes and the autoload feature.

--
Jaap Taal
ulderico at maber dot com dot br
03-Mar-2005 08:19
For those who misses Java-like import , here's a handy implemention.

It may not well handle Singletons and References,
but I think it'll do fine for most cases...

Hey people do not steal it,
keep it GNU GPL please...

The following code is preseverd by GNU GPL:

function import($class){
  $base_class = "PATHTO/WEB-INF/classes";
  $class_strut = explode(".",$class);
  if(sizeof($class_strut)<1)
   trigger_error("too short class invocation");
      

  if($class_strut[sizeof($class_strut)-1] == "*"){
   $tmp_class_strut = $class_strut;
   unset($tmp_class_strut[sizeof($tmp_class_strut)-1]);
   $tmp_base_class = $base_class."/".
implode("/",$tmp_class_strut);
   $d = dir($tmp_base_class);
   while (false !== ($entry = $d->read())) {
     if($entry == "." || $entry == "..")
       continue;
     require_once($tmp_base_class."/".$entry);
   }
   $d->close(); // Do you recognize this Example???

  }else{
   if(file_exists($base_class."/".
implode("/",$class_strut).".class.php"))
     require_once($base_class."/".
implode("/",$class_strut).".class.php");
   else
     require_once($base_class."/".
implode("/",$class_strut).".php");
  }

  $vars = get_defined_vars();
  foreach($vars as $var => $value){
   $GLOBALS[$var] = $value;
  }// Hey remember you're inside a function,
  // you've got to globalise value here to outside...
  
}
thomas dot revell at uwe dot ac dot uk
21-Jan-2005 05:00
Regarding the case insensitivity problems on Windows, it looks to me as though it is a problem in PHP5 as well (at least in some cases).

The following gave me problems:

From file URLSwitcher.php
<?php
require_once 'slimError/slimError.php';
require_once
'Navigator_Cache.php';
....
?>

From file Navigator_Cache.php
<?php
require_once 'slimError/slimerror.php';
...
?>

From file slimerror.php
<?php
class SLIMError {
...
}
?>
The above setup gave me an error : "Cannot redeclare class SLIMError"

If I change the require_once in URLSwitcher.php to match the one in Navigator_Cache.php, there isn't a problem, but if I do this the other way round, the same problem occurs.
18-Mar-2004 12:49
> Mac OS X systems are also not case-sensitive.
That depends on the filesystem:
- HFS and HFS+ are NOT case sensitive.
- UFS is case sensitive.
jaisen - at - jmathai - dot - com
12-Mar-2004 03:16
NOTE: This function changed how it worked.  In PHP 3 this behaved very differently than it does on PHP 4.  Require used to include and parse the file regardless where the require line was positioned.

For example (PHP3):

<?php
 
if(false){ require_once 'file_does_not_exist.php'; }
?>

That code throw a fatal exception even though it's in a conditional block which evaluates to false.  In PHP 4 the file is never included or parsed, so no exception is thrown.

For example (PHP4)
<?php
 
if(false){ require_once '1_file_does_not_exists.php'; }
  require_once
'2_file_does_not_exists.php';
?>

Stops execution of the script on trying to require the 2nd file...by bypasses the first require.

--JM

<includeinclude_once>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 18:35:34 2005 EDT