• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php引入文件的方法有哪些

php 搞代码 4年前 (2022-01-04) 18次浏览 已收录 0个评论

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

区别介绍:

include和require

include有返回值,而require没有返回值。

include在加载文件失败时,会生成一个警告(E_WARNING),在错误发生后脚本继续执行。所以include用在希望继续执行并向用户输出结果时。

//test1.php<?phpinclude './tsest.php';echo 'this is test1';?>//test2.php<?phpecho 'this is test2\n';function test() {    echo 'this is test\n';}?>//结果:this is test1

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

//test1.php<?phprequire './tsest.php';echo 'this is test1';?>//test2.php<?phpecho 'this is test2\n';function test() {    echo 'this is test\n';}?>

结果:

include和include_once

include载入的文件不会判断是否重复,只要有include语句,就会载入一次(即使可能出现重复载入)。而include_once载入文件时会有内部判断机制判断前面代码是否已经载入过。

这里需要注意的是include_once是根据前面有无引入相同路径的文件为判断的,而不是根据文件中的内容(即两个待引入的文件内容相同,使用include_once还是会引入两个)。

//test1.php<?phpinclude './test2.php';echo 'this is test1';include './test2.php';?>//test2.php<?phpecho 'this is test2';?>//结果:this is test2this is test1this is test2//test1.php<?phpinclude './test2.php';echo 'this is test1';include_once './test2.php';?>//test2.php<?phpecho 'this is test2';?>//结果:this is test2this is test1//test1.php<?phpinclude_once './test2.php';echo 'this is test1';include './test2.php';?>//test2.php<?phpecho 'this is test2';?>//结果:this is test2this is test1this is test2//test1.php<?phpinclude_once './test2.php';echo 'this is test1';include_once './test2.php';?>//test2.php<?phpecho 'this is test2';?>//结果:this is test2this is test1

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

来源gaodaimacom搞#^代%!码网更多相关教程请访问搞代码

以上就是php引入文件的方法有哪些的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php引入文件的方法有哪些

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址