1,require是无条件包含也就是如果一个流程里加入require,无论条件成立与否都会先执行require这个已经不适用了,因为require 可以包含变量指向的文件如if($a = 1){
$file = '1.php';
}else{
$file = '2.php';
}
require($file);2,包含文件不存在或者语法错误的时候require是致命的, include不是3,include有返回值,而require没有(可能因为如此require的速度比include快)
$login = include('test.php');
if(!empty($login)){
echo "文件包含成功";
}else{
echo "文件包含失败";
}
引用文件的方法有两种:require 及 include。两种方式提供不同的使用弹性。require 的使用方法如 require("MyRequireFile.php"); 。这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指定引入的文件,使它变成 PHP 程序网页的一部份。常用的函数,亦可以这个方法将它引入网页中。include 使用方法如 include("MyIncludeFile.php"); 。这个函数一般是放在流程控制的处理部分中。PHP 程序网页在读到 include 的文件时,才将它读进来。这种方式,可以把程序执行时的流程简单化。

解决方案 »

  1.   

    对第一点表示反对,那样写太麻烦了,遇到这种判断包含情况,下面这样写比较方便:
    <?
      if($a==1)
        require '1.php';
      else
        require '2.php';
    ?>
      

  2.   

    楼上犯了一个很大的错误。你上面这个逻辑无论是否成立,两个程序都会被运行。这是include和require的区别。
      

  3.   

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

  4.   

    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
      

  5.   

    Stops execution of the script on trying to require the 2nd file...by bypasses the first require.
    停止对require第二个脚本的尝试执行,通过迂回第一个脚本。不懂什么意思。
      

  6.   

    <?
      if($a==1)
        require '1.php';
      else
        require '2.php';
    ?>
    ----------------------------------------------
    楼上犯了一个很大的错误。你上面这个逻辑无论是否成立,两个程序都会被运行。这是include和require的区别。
    ----------------------------------------------
    你怎么知道这两个程序都被运行了?
    我在1.php输出"aaaaaaaaaaaa",在2.php中输出"bbbbbbbbbbbbbbbbb",按照你说的两个程序都会被运行,岂不是要输出"aaaaaaaaaaaabbbbbbbbbbbbbbbbb",但是实际上,只是按照判断输出相应包含文件的内容
    强烈抗议某些人不测试附合别人说
      

  7.   

    1.据我测试,include要比require快
    2.include用来包含有php程序的文件,文本文件用readfile,require完全不用以上仅代表我的个人观点
      

  8.   

    我比较喜欢用include....因为require这个单词后面几个字母我经常输入错误。
      

  9.   

    倒,那你用editplus自动完成啊
    设置一下php.acp
    #T=inc
    include(^!)
    #T=req
    require(^!)
    #T=rea
    readfile(^!)
    输入inc 空格 或 req 空格 直接出来了
      

  10.   

    高;
    一直概念include速度优于require;Stops execution of the script on trying to require the 2nd file...by bypasses the first require.P:
    在尝试越过第一个require而直接进行第二文件时停止执行命令;
      

  11.   

    所谓require的无条件引入,不知道是否可以这样理解,将php的执行方式看作类似于编译程序的方式,
    即require是在编译过程中就已引入,而include却是在编译后按照脚本方式解释执行时才会起作用,
    若通过条件判断语句来控制include语句的执行,即是否引入,是可行的。而require语句不管外围是否有条件判断语句,在编译过程中都会被引入,外围的条件判断只会对是否执行被引入的文件(已经引入)的内部逻辑产生影响。还请高手们详细的解释一下。
    这个命题经常在PHP面试题中出现!
      

  12.   

    1.据我测试,include要比require快
    -----------
    测试数据呢?
    我的看法
    include 如何 include 一个不存在的页面。php 代码会继续执行
    require 就会停止,不执行以后的代码了。