四种语句
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作用雷同,差别的是他们在实行到时会先搜检目的内容是不是是在之前已导入过,假如导入过了,那末便不会再次反复引入其一样的内容。
相干引荐:《php教程》
互相区分
include和require:
include有返回值,而require没有返回值。
include在加载文件失利时,会生成一个正告(E_WARNING),在毛病发作后剧本继承实行。所以include用在愿望继承实行并向用户输出效果时。
//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),在毛病发作后剧本住手实行。平常用在后续代码依赖于载入的文件的时刻。
//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照样会引入两个)。
//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的区分雷同。
以上就是php文件包括的几种要领的细致内容,更多请关注ki4网别的相干文章!