php文件包含的几种方式

王林
Lepaskan: 2023-02-24 12:08:01
asal
4335 orang telah melayarinya

php文件包含的几种方式

四种语句

PHP中有四个加载文件的语句:includerequireinclude_oncerequire_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用在希望继续执行并向用户输出结果时。

//test1.php
<?php
include &#39;./tsest.php&#39;;
echo &#39;this is test1&#39;;
?>

//test2.php
<?php
echo &#39;this is test2\n&#39;;
function test() {
    echo &#39;this is test\n&#39;;
}
?>

//结果:
this is test1
Salin selepas log masuk

require在加载失败时会生成一个致命错误(E_COMPILE_ERROR),在错误发生后脚本停止执行。一般用在后续代码依赖于载入的文件的时候。

//test1.php
<?php
require &#39;./tsest.php&#39;;
echo &#39;this is test1&#39;;
?>

//test2.php
<?php
echo &#39;this is test2\n&#39;;
function test() {
    echo &#39;this is test\n&#39;;
}
?>
Salin selepas log masuk

结果:

引入文件.png

include和include_once:

include载入的文件不会判断是否重复,只要有include语句,就会载入一次(即使可能出现重复载入)。而include_once载入文件时会有内部判断机制判断前面代码是否已经载入过。这里需要注意的是include_once是根据前面有无引入相同路径的文件为判断的,而不是根据文件中的内容(即两个待引入的文件内容相同,使用include_once还是会引入两个)。

//test1.php
<?php
include &#39;./test2.php&#39;;
echo &#39;this is test1&#39;;
include &#39;./test2.php&#39;;
?>

//test2.php
<?php
echo &#39;this is test2&#39;;
?>

//结果:
this is test2this is test1this is test2


//test1.php
<?php
include &#39;./test2.php&#39;;
echo &#39;this is test1&#39;;
include_once &#39;./test2.php&#39;;
?>

//test2.php
<?php
echo &#39;this is test2&#39;;
?>

//结果:
this is test2this is test1


//test1.php
<?php
include_once &#39;./test2.php&#39;;
echo &#39;this is test1&#39;;
include &#39;./test2.php&#39;;
?>

//test2.php
<?php
echo &#39;this is test2&#39;;
?>

//结果:
this is test2this is test1this is test2


//test1.php
<?php
include_once &#39;./test2.php&#39;;
echo &#39;this is test1&#39;;
include_once &#39;./test2.php&#39;;
?>

//test2.php
<?php
echo &#39;this is test2&#39;;
?>

//结果:
this is test2this is test1
Salin selepas log masuk

require和require_once:同include和include_once的区别相同。

推荐教程:PHP视频教程

Atas ialah kandungan terperinci php文件包含的几种方式. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!