最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

php引入文件的方法有哪些

来源:动视网 责编:小采 时间:2020-11-03 12:31:28
文档

php引入文件的方法有哪些

php引入文件的方法有哪些:PHP中引入文件的方法有:include、require、include_once、require_once。区别介绍:include和requireinclude有返回值,而require没有返回值。include在加载文件失败时,会生成一个警告(E_WARNING),在错误发生后脚本继续执行。所以include
推荐度:
导读php引入文件的方法有哪些:PHP中引入文件的方法有:include、require、include_once、require_once。区别介绍:include和requireinclude有返回值,而require没有返回值。include在加载文件失败时,会生成一个警告(E_WARNING),在错误发生后脚本继续执行。所以include


PHP中引入文件的方法有:include、require、include_once、require_once。

区别介绍:

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
';
function test() {
 echo 'this is test
';
}
?>

//结果:
this is test1

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

//test1.php
<?php
require './tsest.php';
echo 'this is test1';
?>

//test2.php
<?php
echo 'this is test2
';
function test() {
 echo 'this is test
';
}
?>

结果:

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的区别相同。

更多相关教程请访问Gxlcms。

文档

php引入文件的方法有哪些

php引入文件的方法有哪些:PHP中引入文件的方法有:include、require、include_once、require_once。区别介绍:include和requireinclude有返回值,而require没有返回值。include在加载文件失败时,会生成一个警告(E_WARNING),在错误发生后脚本继续执行。所以include
推荐度:
标签: 文件 有什么 方法
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top