在PHP中:
什么时候应该使用require和include? 什么时候应该使用require_once vs. include_once?
在PHP中:
什么时候应该使用require和include? 什么时候应该使用require_once vs. 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 -如果文件不存在,那么页面将继续加载。
Use
需要 当你的应用程序需要这个文件时,例如一个重要的消息模板或一个包含配置变量的文件,如果没有这些文件应用程序将会崩溃。 require_once 当文件包含的内容会在后续包含时产生错误时,例如。 Function important() {/* important code */}在你的应用程序中肯定是需要的,但由于函数不能被重新声明,所以不应该再次包含。 包括 当文件不是必需的,应用程序流程应该继续时,没有找到,例如 对于模板引用当前作用域的变量或其他东西非常有用 include_once 可选的依赖关系,会在后续加载时产生错误,或者可能远程文件包含,由于HTTP开销,您不希望发生两次
但基本上,什么时候用哪种取决于你。
我使用的函数如下:
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? require和include函数执行相同的任务,即包含并计算指定的文件,但不同的是,当指定的文件位置无效或任何错误时,require将导致致命错误,而include将生成警告并继续代码执行。 因此,当您试图包含的文件是系统的核心,可能会对其余代码造成巨大影响时,您可以使用require函数;当您试图包含的文件是一个包含一些不太重要的代码的简单文件时,您可以使用include函数。 我个人的建议(对于不太重要的代码)是在开发阶段在代码中到处使用require函数,这样你就可以调试代码,然后在将其转移到生产环境之前用include函数替换所有的require函数,这样即使你遗漏了任何错误,也不会影响最终用户,其余的代码也能正常执行…… 什么时候应该使用require_once或require? require和require_once之间的基本区别是require_once将检查文件是否已经包含,如果已经包含,则不包含该文件,而require函数将包括该文件,而不管文件是否已经包含。 因此,当你想要一次又一次地包含某段代码时,使用require函数,而如果你只想在代码中包含某段代码一次,则使用require_once。
无论何时使用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将一次包含该文件,但会停止进一步执行。