四种语句
PHP中有四个加载文件的语句:include、require、include_once、require_once。
基本语法
require:require函数一般放在PHP脚本的最前面,PHP执行前就会先读入require指定引入的文件,包含并尝试执行引入的脚本文件。require的工作方式是提高PHP的执行效率,当它在同一个网页中解释过一次后,第二次便不会解释。但同样的,正因为它不会重复解释引入文件,所以当PHP中使用循环或条件语句来引入文件时,需要用到include。
include:可以放在PHP脚本的任意位置,一般放在流程控制的处理部分中。当PHP脚本执行到include指定引入的文件时,才将它包含并尝试执行。这种方式可以把程序执行时的流程进行简单化。当第二次遇到相同文件时,PHP还是会重新解释一次,include相对于require的执行效率下降很多,同时在引入文件中包含用户自定义函数时,PHP在解释过程中会发生函数重复定义问题。
require_once / include_once:分别与require / include作用相同,不同的是他们在执行到时会先检查目标内容是不是在之前已经导入过,如果导入过了,那么便不会再次重复引入其同样的内容。
相互区别
include和require:
include有返回值,而require没有返回值。
include在加载文件失败时,会生成一个警告(E_WARNING),在错误发生后脚本继续执行。所以include用在希望继续执行并向用户输出结果时。
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
						  | 
//test1.php
<?php
include './tsest.php';
echo 'this is test1';
?>
//test2.php
<?php
echo 'this is test2\\n';
function test() {
echo 'this is test\\n';
}
?>
//结果:
this is test1
 | 
require在加载失败时会生成一个致命错误(E_COMPILE_ERROR),在错误发生后脚本停止执行。一般用在后续代码依赖于载入的文件的时候。
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
						  | 
//test1.php
<?php
require './tsest.php';
echo 'this is test1';
?>
//test2.php
<?php
echo 'this is test2\\n';
function test() {
echo 'this is test\\n';
}
?>
 | 
结果:
include和include_once:
include载入的文件不会判断是否重复,只要有include语句,就会载入一次(即使可能出现重复载入)。而include_once载入文件时会有内部判断机制判断前面代码是否已经载入过。这里需要注意的是include_once是根据前面有无引入相同路径的文件为判断的,而不是根据文件中的内容(即两个待引入的文件内容相同,使用include_once还是会引入两个)。
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
 
								26
 
								27
 
								28
 
								29
 
								30
 
								31
 
								32
 
								33
 
								34
 
								35
 
								36
 
								37
 
								38
 
								39
 
								40
 
								41
 
								42
 
								43
 
								44
 
								45
 
								46
 
								47
 
								48
 
								49
 
								50
 
								51
 
								52
 
								53
 
								54
 
								55
 
								56
 
								57
 
								58
 
								59
 
								60
 
								61
 
								62
 
								63
 
								64
 
								65
 
								66
 
								67
 
								68
 
								69
 
								70
 
								71
 
								72
 
								73
 
								74
 
								75
 
								76
 
								77
 
								78
 
								79
 
								80
 
								81
 
								82
 
								83
 
								84
 
								85
 
								86
 
								87
 
								88
 
								89
 
								90
 
								91
 
								92
 
								93
 
								94
 
								95
 
								96
 
								97
 
								98
 
								99
 
								100
 
								101
 
								102
 
								103
 
								104
 
								105
 
								106
 
								107
 
								108
 
								109
 
								110
 
								111
 
								112
 
								113
 
								114
 
								115
 
								116
 
								117
 
								118
 
								119
 
								120
 
								121
 
								122
 
								123
						  | 
//test1.php
<?php
include './test2.php';
echo 'this is test1';
include './test2.php';
?>
//test2.php
<?php
echo 'this is test2';
?>
//结果:
this is test2this is test1this is test2
//test1.php
<?php
include './test2.php';
echo 'this is test1';
include_once './test2.php';
?>
//test2.php
<?php
echo 'this is test2';
?>
//结果:
this is test2this is test1
//test1.php
<?php
include_once './test2.php';
echo 'this is test1';
include './test2.php';
?>
//test2.php
<?php
echo 'this is test2';
?>
//结果:
this is test2this is test1this is test2
//test1.php
<?php
include_once './test2.php';
echo 'this is test1';
include_once './test2.php';
?>
//test2.php
<?php
echo 'this is test2';
?>
//结果:
this is test2this is test1
 | 
require和require_once:同include和include_once的区别相同。
以上就是本次介绍的全部知识点内容,感谢大家对快网idc的支持。
相关文章
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
 - 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
 - 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
 - ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
 - ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
 
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
 - 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
 - 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
 - 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
 - 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
 
快网idc优惠网
QQ交流群
- 
            2025-05-29 49
 - 
            2025-05-27 46
 - 
            
Java源码解析CopyOnWriteArrayList的讲解
2025-05-29 85 - 
            2025-05-25 75
 - 
            2025-05-25 20
 
        

    		
            	
        
        