在PHP中:

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


当前回答

1 -如果文件不是,"require"和"require_once"会抛出致命错误 存在并停止脚本执行

2 - "include"和"include_once"抛出警告并执行 仍在继续

3 - "require_once"和"include_once",顾名思义, 如果文件已经包含,则将不包含该文件 "require", "require_once", "include"或"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)它。

_once函数与不含_once函数的区别: 没有_once函数的代码将被再次包含,而有_once函数的PHP会跟踪被包含的文件,并且只包含它一次。

require和include的区别: 如果没有找到所需的文件,PHP将发出致命错误,而for include只会发出警告。

在PHP中,有4个类似的函数用于包含和要求文件:

include(): include()语句用于将一个文件的内容包含到另一个文件中。如果无法找到包含的文件,将生成警告,但脚本将继续执行。

require(): require()语句类似于include,但是如果找不到文件,它会生成一个致命错误。脚本将立即停止执行。

include_once(): include_once()语句与include相同,但它只包含文件一次,即使在同一个脚本中包含多次。

require_once(): require_once()语句与require相同,但它只包含文件一次,即使它在同一个脚本中包含多次。

简单来说:

当您希望包含一个文件并在文件丢失的情况下继续执行脚本时,使用include()。

当您需要包含一个文件并在文件丢失时停止执行脚本时,请使用require()。

当您希望只包含一次文件时,请使用include_once()或require_once(),以避免出现重复函数或变量的问题。

注意:一般情况下,建议使用require_once()而不是include_once()来确保所需的文件被包含并且不会丢失。

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

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

例子:

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

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

Include_once 'filename'

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