php如何实现include_once

php如何实现include_once

内容导读

收集整理的这篇技术教程文章主要介绍了php如何实现include_once,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1807字,纯文字阅读大概需要3分钟

内容图文

include_once 语句在脚本执行期间包含并运行指定文件。此行为和 include 语句类似,唯一区别是如果该文件中已经被包含过,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。

include_once 可以用于在脚本执行期间同一个文件有可能被包含超过一次的情况下,想确保它只被包含一次以避免函数重定义,变量重新赋值等问题。

Note:(推荐学习:PHP编程从入门到精通)

在 PHP 4中,_once 的行为在不区分大小写字母的操作系统(例如 Windows)中有所不同,例如:

include_once 在 PHP 4 运行于不区分大小写的操作系统中

<?phpinclude_once "a.php"; // 这将包含 a.phpinclude_once "A.php"; // 这将再次包含 a.php!(仅 PHP 4)?>

此行为在 PHP 5 中改了,例如在 Windows 中路径先被规格化,因此 C:PROGRA~1A.php 和 C:Program Filesa.php 的实现一样,文件只会被包含一次。

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

以上就是php如何实现include_once的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的php如何实现include_once全部内容,希望文章能够帮你解决php如何实现include_once所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com