PHP函数include()的重要性分析_PHP教程

php中文网
发布: 2016-07-15 13:29:58
原创
1641人浏览过

降重鸟
降重鸟

要想效果好,就用降重鸟。AI改写智能降低AIGC率和重复率。

降重鸟 113
查看详情 降重鸟

在我们开发一个网站的时候,必须要考虑到代码重用的问题, 不管你的网站规模的大小,也不管你维护和更新的次数,这一问题都是非常麻烦的。我们今天就向大家介绍这方面的解决办法,那就是php函数include()的具体应用。

  • 中,至少有几个函数可以帮助你实现代码重用的目的,所使用的函数取决于你所重用的代码,主要的函数有:

    * include() and include_once()

    * require() and require_once()

    PHP函数include()包含并对给定的文件进行计算,例如:

    立即学习PHP免费学习笔记(深入)”;

    include('/home/me/myfile');

    在include文件中的任何代码都在PHP函数include()出现的代码范围内执行,你可以通过联合使用include()和fopen()在自己的服务器上包含静态文件,在另一台服务器上包含目标文件。

    include_once()的功能与include()相同,二者之间的区别在于它会检查一个文件中的代码是否已经包含在现有的脚本中,如果代码已经存在,则不会再次包含它。

    require()函数用给定文件的内容取代它本身,这一代替过程发生在PHP引擎编译代码期间,而不是在执行期间进行,它不象PHP函数include()那样会首先进行计算。require()函数更多地用在静态元素中,而include()更多地用于动态元素中。与include_once()类似的是,require_once()将首先检查是否已经插入给定的代码,如果代码已经存在,就不再插入了。

    为也了解其内容,在版权信息、静态文字和其他不包含变量的元素或者依赖其他正在执行的脚本的元素中我更趋向于使用require函数。例如:

    <OL class=dp-xml><LI class=alt><SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>Something</SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>[a lot of content]   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699><?</FONT></STRONG></SPAN><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>// insert copyright   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>require('/home/me/mycopyright');   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>  </SPAN></LI></OL>
    登录后复制

    另一方面,我经常在文件的开始使用PHP函数include()来控制许多的函数:

    <OL class=dp-xml><LI class=alt><SPAN><SPAN class=tag><STRONG><FONT color=#006699><?</FONT></STRONG></SPAN><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>//得到函数库   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>include('/home/me/myfunctions');   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>// do PHP things with my functions </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>Something</SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>[a lot of content]   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>  </SPAN></SPAN></LI></OL>
    登录后复制

    下一个问题就该是“include和require文件在哪里?”,对这个问题简单的回答是,“系统中的任意地方。”如果你的代码中包含有带有用户名和口令的数据库连接,你肯定不会将它们都放在文档根目录中向所有的人都开放。

    included或required文件可以在系统上的任何地方,只要PHP运行的系统上的用户可以访问这些文件即可,你可以使这些文件具有任何后缀,或者不使用后缀。

    使用PHP函数include()和require()对在网站中的元素进行具体化是一种普遍的现象,并在需要对网站升级时,给你带来很大的方便。


    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446333.htmlTechArticle在我们开发一个网站的时候,必须要考虑到代码重用的问题,不管你的网站规模的大小,也不管你维护和更新的次数,这一问题都是非常麻...
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号