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

require()

require() 语句包括并运行指定文件。

require() 语句包括并运行指定文件。有关包括如何工作的详细信息见 include() 的文档。

require()include() 除了怎样处理失败之外在各方面都完全一样。include() 产生一个警告require() 则导致一个致命错误。换句话说,如果你想在丢失文件时停止处理页面,那就别犹豫了,用 require() 吧。include() 就不是这样,脚本会继续运行。同时也要确认设置了合适的include_path

例子 16-2. 基本的 require() 例子

<?php

require 'prepend.php';

require
$somefile;

require (
'somefile.txt');

?>

更多例子参见 include() 文档。

注: 在 PHP 4.0.2 之前适用以下规则:require() 总是会尝试读取目标文件,即使它所在的行根本就不会执行。条件语句不会影响 require()。不过如果 require() 所在的行没有执行,则目标文件中的代码也不会执行。同样,循环结构也不影响 require() 的行为。尽管目标文件中包含的代码仍然是循环的主体,但 require() 本身只会运行一次。

注: 由于这是一个语言结构而非函数,因此它无法被“变量函数”调用。

警告

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

参见 include()require_once()include_once()eval()file()readfile()virtual()include_path




add a note add a note User Contributed Notes
require
jaisen - at - jmathai - dot - com
13-Mar-2004 04:10
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 '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 '1_file_does_not_exists.php'; }
 require '2_file_does_not_exists.php';
?>

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

--JM

<returninclude>
 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