在PHP中:

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


当前回答

区别在于命令生成的错误。使用require,您想要使用的文件确实是必需的,因此如果找不到它,将生成一个E_ERROR。

require()与include()相同,除了失败时它也会产生致命的E_ERROR级别错误。

include只在失败时生成一个E_WARNING错误,该错误或多或少是沉默的。

因此,如果需要该文件才能使其余代码正常工作,并且您希望脚本失败,则使用该文件。


* _once ():

Include_once()可以在脚本的特定执行过程中多次包含和计算相同文件的情况下使用,因此在这种情况下,它可以帮助避免诸如函数重新定义、变量值重新赋值等问题。

当然,同样适用于require_once()。


引用:require(), include_once()

其他回答

我注意到的一件事是,当使用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和include_once。

所以你的问题应该是…

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

这里描述了1的答案。

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

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

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

我使用的函数如下:

function doSomething() {
    require_once(xyz.php);
    ....
}

在xyz.php中声明了常量值。

我必须从另一个PHP脚本文件调用这个doSomething()函数。

但是我观察到在循环中调用此函数时的行为,对于第一次迭代doSomething()在xyz.php中获得常量值,但后来每次迭代doSomething()都无法获得xyz.php中声明的常量值。

我通过从require_once()切换到include()来解决我的问题,更新的doSomething()代码如下:

function doSomething() {
    include(xyz.php);
    ....
}

现在对doSomething()的每次迭代调用都得到xyz.php中定义的常量值。

只需使用require和include。

因为想想如何使用include_once或require_once。 查找保存包含的或必需的PHP文件的日志数据。 这比include和require要慢。

if (!defined(php)) {
    include 'php';
    define(php, 1);
}

就像这样用…

无论何时使用require_once(),当您在当前文件中只需要被调用的文件一次时,可以在文件中使用require_once()来包含另一个文件。 在这个例子中,我有一个test1.php。

<?php  
echo "today is:".date("Y-m-d");  
?>  

在另一个文件中,命名为test2.php

<?php  
require_once('test1.php');  
require_once('test1.php');  
?>

当你看到m需要test1文件两次,但该文件将包含test1一次,而在第二次调用时,这将被忽略。并且不停止将显示输出一次。

当你在使用'include_once() '时,当你在当前文件中需要调用的文件不止一次时,可以在一个文件中使用'include_once() '来包含另一个文件。 在本例中,我有一个名为test3.php的文件。

<?php  
echo "today is:".date("Y-m-d");  
?> 

在另一个文件中,命名为test4。php

<?php  
include_once('test3.php');  
include_once('test3.php');  
?>

在您观察时,包含test3文件的m将一次包含该文件,但会停止进一步执行。