Php require、include、require\u once和include\u once之间的区别?
在PHP中:Php require、include、require\u once和include\u once之间的区别?,php,include,require,require-once,Php,Include,Require,Require Once,在PHP中: 我应该在什么时候使用require与include 我应该在什么时候使用require\u once与include\u once 有要求和包含一次 所以你的问题应该是 我应该在什么时候使用require与include 我应该在什么时候使用require\u一次vs.require 对1的答案进行了描述 require()函数与include()相同,只是它处理错误的方式不同。如果发生错误,include()函数将生成警告,但脚本将继续执行。require()生成一个致命错误,
- 我应该在什么时候使用
与require
include
- 我应该在什么时候使用
与require\u once
include\u once
要求
和包含一次
所以你的问题应该是
require
与include
require\u一次
vs.require
_once函数与未使用_once函数的区别: 如果没有_once,代码将再次包含,而使用_once函数,PHP将跟踪包含的文件,并且只包含一次 要求和要求之间的差异包括:
如果找不到所需的文件,PHP将发出致命错误,而对于include,只会发出警告。不同之处在于命令生成的错误。使用
require
,您想要使用的文件确实是必需的,因此如果找不到它,就会生成E_错误
require()
与include()
完全相同,除非出现故障,否则还会产生致命的E_错误
级别错误
include
仅在失败时生成一个或多或少无声的E_警告
错误
因此,如果需要该文件才能使其余代码正常工作,并且您希望脚本失败,则使用该文件,因为该文件不可用
对于*\u once()
:
include_once()
可用于在脚本的特定执行过程中多次包含和计算同一文件的情况,因此在这种情况下,它可能有助于避免函数重新定义、变量值重新分配等问题
当然,这同样适用于require\u once()
参考:,要求文件必须存在,如果不存在,则会显示错误;而使用include-如果文件不存在,则页面将继续加载。使用
当应用程序需要该文件时,例如,重要消息模板或包含配置变量的文件,如果没有这些配置变量,应用程序将中断
当文件包含可能在后续包含时产生错误的内容时,例如。
function important(){/*important code*/}
在应用程序中肯定是必需的,但由于无法重新声明函数,因此不应再次包含
-
当文件不需要时,应用程序流应在未找到时继续,例如
非常适合引用当前范围或其他变量的模板
可选的依赖项,这些依赖项会在后续加载时产生错误,或者可能会导致远程文件包含,由于HTTP开销,您不希望发生两次这种情况
但基本上,什么时候使用哪个取决于你。我的建议是只使用一次require\u
99.9%
相反,使用require
或include
意味着您的代码在其他地方不可重用,即您正在拉入的脚本实际上执行代码,而不是提供类或某些函数库
如果您需要/包括现场执行的代码,那就是过程代码,您需要了解新的范例。类似于面向对象编程、基于函数的编程或函数式编程
如果您已经在进行OO或函数式编程,那么使用include\u once
通常会延迟堆栈中发现bug/错误的位置。您想知道函数Do\u cool\u stuff()
在您稍后调用时不可用,还是在您希望它可用时需要库?通常,如果您需要或期望的东西不可用,最好立即知道,所以只需使用一次require\u
或者,在现代OOP中,使用时只需要类。需要关键部分,如授权,并包括所有其他部分
多重包含是非常糟糕的设计,必须避免。所以,*\u一次并不重要。使用“包含”可重用模板。对所需的库使用“require”
“*\u once”很好,因为它检查文件是否已加载,但它仅在“require\u once”中对我有意义
require()
与include()
完全相同,除非出现故障,否则还会产生致命的E\u编译错误
级别错误。换句话说,它将停止脚本,而include()
只发出一个警告(E_warning
),允许脚本继续
\u once()
变体也是如此。include()
如果不能包含文件,将抛出警告,但脚本的其余部分将运行
require()
include_once()
和require_once()
函数将不会再次包含文件(如果已经包含该文件)
请参阅以下文档页面:
/您也可以多次包含同一文件:<?php
echo "today is:".date("Y-m-d");
?>
<?php
require_once('test1.php');
require_once('test1.php');
?>
<?php
echo "today is:".date("Y-m-d");
?>
<?php
include_once('test3.php');
include_once('test3.php');
?>
if (!defined(php)) {
include 'php';
define(php, 1);
}
function doSomething() {
require_once(xyz.php);
....
}
function doSomething() {
include(xyz.php);
....
}
<?php
// arr.php
return ['x'=>'y'];
<?php
//main.php
$arr= require 'arry.php'
Require 'filename'
Require_once 'filename'
Include 'filename'
Include_once 'filename'
if(file_exists($RequiredFile)){
require_once($RequiredFile);
}else{
die('Error: File Does Not Exist');
}
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
//my site configuration file
//For Database
$location='localhost';
$dbuser='yourname';
$userpw='yourpassword';
$database='nameofdatabase';
?>
<?php
class UsedInLoop {
public static function databaseQuery(){
require(/path/to/conf.php); //require_once will not work here
$db = new mysqli($location, $dbuser, $userpw, $database);
//yada yada yada
}
}
?>
<?php
require_once('path/to/arbitraryObject.php'); //either will likely be OK at this level
$obj = new arbitraryObject();
foreach($array as $element){
$obj->myFunction();
}
?>
<?php
class arbitraryObject {
public function myFunction(){
require_once(/path/to/UsedInLoop.php); //This must be require_once. require() will not work
UsedInLoop::databaseQuery();
}
}
?>
<?php
require(/path/to/UsedInLoop.php); //now require() is fine
class arbitraryObject {
public function myFunction(){
UsedInLoop::databaseQuery();
}
}
?>