background image

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

Fatal  error:  require()  [function.require]:  Failed  opening  required  ‘test3.php' 

(include_path='.;C:\php5\pear') in D:\WebSite\test.php on line 2