background image

$i = 1;
while ($i < 3) {
include("somefile.$i");
$i++;
}

2. 执行时报错方式不同

include 和 require 的区别:include 引入文件的时候,如果碰到错误,会给出提示,并继续运
行下边的代码,

require 引入文件的时候,如果碰到错误,会给出提示,并停止运行下边的

代码。例如下面例子:

 
写两个

php 文件,名字为 test1.php  和 test2.php,注意相同的目录中,不要存在一个名字是

test3.php 的文件。
test1.php
代码如下

:

<?PHP
include  (

”test3.php”);

echo  

“abc”;

?>

test2.php
代码如下

:

<?PHP
require (

”test3.php”)

echo  

“abc”;

?>

浏览第一个文件,因为没有找到

test999.php 文件,我们看到了报错信息,同时,报错信息

的下边显示了

abc,你看到的可能是类似下边的情况:

Warning: include(test3.php) [function.include]: failed to open stream: No such file or directory in 
D:\WebSite\test.php on line 2

Warning: 

include() 

[function.include]: 

Failed 

opening 

‘test3.php' 

for 

inclusion 

(include_path='.;C:\php5\pear') in D:\WebSite\test.php on line 2
abc (下面的被执行了)

浏览第二个文件,因为没有找到

test3.php 文件,我们看到了报错信息,但是,报错信息的

下边没有显示

abc,你看到的可能是类似下边的情况:

Warning: require(test3.php) [function.require]: failed to open stream: No such file or directory in 
D:\WebSite\test2.php on line 2