在PHP中:

什么时候应该使用require和include? 什么时候应该使用require_once vs. include_once?


当前回答

还有require和include_once。

所以你的问题应该是…

什么时候应该使用require和include? 什么时候应该使用require_once vs. require

这里描述了1的答案。

require()函数与include()函数相同,只是处理错误的方式不同。如果发生错误,include()函数将生成警告,但脚本将继续执行。require()生成一个致命错误,脚本将停止。

2的答案可以在这里找到。

require_once()语句与require()语句相同,只是PHP会检查文件是否已经包含,如果已经包含,则不会再次包含(require)它。

其他回答

7年后的2018年,答案揭晓

这个问题是七年前提出的,没有一个答案能对这个问题提供实际帮助。在现代PHP编程中,您主要只使用require_once一次来包含自动加载器类(通常是编写器自动加载器),它将加载所有的类和函数(函数文件需要显式地添加到编写器中。Json文件可在所有其他文件)。如果你的类不能从自动加载器加载,你可以使用require_once来加载它。

有些场合我们需要使用require。例如,如果你有一个非常大的数组定义,而你不想把它添加到你的类定义源代码中,你可以:

 <?php
// arry.php
return ['x'=>'y'];

 <?php
//main.php
$arr= require 'arry.php'

如果你打算包含的文件包含一些可执行的文件或声明了一些变量,你几乎总是需要使用require,因为如果你使用require_once除了第一个地方,你的代码将不会被执行和/或你的变量将不会无声地初始化,导致绝对难以精确定位的错误。

实际上,include和include_once没有实际用例。

基本上,如果您需要一个错误的路径,PHP会抛出一个致命错误,并调用shutdown函数,但是当您包含一个错误的路径时,PHP将继续执行,但它只会显示一个警告,表明文件不存在。

根据英文单词require, PHP被告知页面或文件的执行取决于所需的文件。

根据我的经验,通常需要重要的文件,如配置文件、数据库类和其他重要的实用程序。

我注意到的一件事是,当使用include时,我只能从包含它的文件中访问包含的文件函数。使用require_once,我可以在第二个required_once文件中运行该函数。

我建议添加

if(file_exists($RequiredFile)){
    require_once($RequiredFile);
}else{
  die('Error: File Does Not Exist');
}

因为当require_once杀死页面时,它有时会返回你的网站文件目录

下面是我做的一个自定义函数来要求文件:

function addFile($file, $type = 'php', $important=false){
    //site-content is a directory where I store all the files that I plan to require_once
    //the site-content directory has "deny from all" in its .htaccess file to block direct connections
    if($type && file_exists('site-content/'.$file.'.'.$type) && !is_dir('site-content/'.$file.'.'.$type)){
        //!is_dir checks that the file is not a folder
        require_once('site-content/'.$file.'.'.$type);
        return 'site-content/'.$file.'.'.$type;
    }else if(!$type && file_exists('site-content/'.$file) && !is_dir('site-content/'.$file)){
        //if you set "$type=false" you can add the file type (.php, .ect) to the end of the "$file" (useful for requiring files named after changing vars)
        require_once('site-content/'.$file);
        return 'site-content/'.$file;
    }else if($important){
        //if you set $important to true, the function will kill the page (which also prevents accidentally echoing the main directory path of the server)
        die('Server Error: Files Missing');
        return false;
    }else{
        //the function returns false if the file does not exist, so you can check if your functions were successfully added
        return false;
    }
}

使用的例子:

$success = addFile('functions/common');

if($success){
    commonFunction();
}else{
    fallbackFunction();
}

它们都是包含文件的方法。

Require意味着它需要它。Require_once意味着它将需要它,但只需要它一次。包含意味着它将包含一个文件,但不需要它继续。

例子:

Require 'filename'
Require_once 'filename'
Include 'filename'

还有一个include_once函数,它只包含一个文件一次。

Include_once 'filename'

当我用手机打字时,不要用大写字母。

还有require和include_once。

所以你的问题应该是…

什么时候应该使用require和include? 什么时候应该使用require_once vs. require

这里描述了1的答案。

require()函数与include()函数相同,只是处理错误的方式不同。如果发生错误,include()函数将生成警告,但脚本将继续执行。require()生成一个致命错误,脚本将停止。

2的答案可以在这里找到。

require_once()语句与require()语句相同,只是PHP会检查文件是否已经包含,如果已经包含,则不会再次包含(require)它。