在PHP中:

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


当前回答

我使用的函数如下:

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,您想要使用的文件确实是必需的,因此如果找不到它,将生成一个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();
}

在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的开销比include大,因为它必须首先解析文件。用包含替换require通常是一种很好的优化技术。

我的建议是99.9%的情况下只使用require_once。

而使用require或include则意味着你的代码在其他地方是不可重用的,也就是说,你正在导入的脚本实际上是在执行代码,而不是提供一个类或一些函数库。

如果你需要/包含当场执行的代码,那就是过程代码,你需要了解一种新的范式。比如面向对象编程,基于函数的编程,或者函数式编程。

如果您已经在进行面向对象或函数式编程,那么使用include_once主要会延迟在堆栈中发现错误/错误的地方。您是否希望在稍后调用do_cool_stuff()函数时知道它不可用,或者在通过要求库而期望它可用的时候知道它不可用?通常,最好立即知道您需要和期望的东西是否不可用,因此只需使用require_once。

或者,在现代OOP中,只需在使用时自动加载你的类。