超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<includeinclude_once>
Last updated: Fri, 22 Jun 2007

require_once()

require_once() 语句在脚本执行期间包括并运行指定文件。此行为和 require() 语句类似,唯一区别是如果该文件中的代码已经被包括了,则不会再次包括。有关此语句怎样工作参见 require() 的文档。

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

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

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

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

例子 16-8. require_once() 在 Windows 下不区分大小写

<?php
require_once("a.php"); // this will include a.php
require_once("A.php"); // this will include a.php again on Windows!
?>

警告

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

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




add a note add a note User Contributed Notes
require_once
jaisen - at - jmathai - dot - com
13-Mar-2004 04: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: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22