仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 679|回复: 8
打印 上一主题 下一主题

[学习教程] PHP编程:php中的include和require的区分

[复制链接]
愤怒的大鸟 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:11:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
不可能吃饭的时候咬了自己一下舌头就从此不吃饭了不是?放下畏惧,继续努力,咱们是来征服它的,而不是被它征服的,振奋起来吧同志。次要存眷白色标志语句便可。
Theinclude(orrequire)statementtakesallthetext/code/markupthatexistsinthespecifiedfileandcopiesitintothefilethatusestheincludestatement.
IncludingfilesisveryusefulwhenyouwanttoincludethesamePHP,HTML,ortextonmultiplepagesofawebsite.
PHPincludeandrequireStatements

ItispossibletoinsertthecontentofonePHPfileintoanotherPHPfile(beforetheserverexecutesit),withtheincludeorrequirestatement.
Theincludeandrequirestatementsareidentical,exceptuponfailure:
<ul>requirewillproduceafatalerror(E_COMPILE_ERROR)andstopthescriptincludewillonlyproduceawarning(E_WARNING)andthescriptwillcontinueSo,ifyouwanttheexecutiontogoonandshowuserstheoutput,eveniftheincludefileismissing,usetheincludestatement.Otherwise,incaseofFrameWork,CMS,oracomplexPHPapplicationcoding,alwaysusetherequirestatementtoincludeakeyfiletotheflowofexecution.Thiswillhelpavoidcompromisingyourapplication"ssecurityandintegrity,justin-caseonekeyfileisaccidentallymissing.
Includingfilessavesalotofwork.Thismeansthatyoucancreateastandardheader,footer,ormenufileforallyourwebpages.Then,whentheheaderneedstobeupdated,youcanonlyupdatetheheaderincludefile.
Syntax

include"filename";

or

require"filename";
PHPincludeExamples

Example1

Assumewehaveastandardfooterfilecalled"footer.php",thatlookslikethis:
Toincludethefooterfileinapage,usetheincludestatement:
Example




Welcometomyhomepage!


Sometext.

Somemoretext.





Runexample?
Example2

Assumewehaveastandardmenufilecalled"menu.php":
AllpagesintheWebsiteshouldusethismenufile.Hereishowitcanbedone(weareusingaelementsothatthemenueasilycanbestyledwithCSSlater):
Example








Welcometomyhomepage!


Sometext.

Somemoretext.




Runexample?
Example3

Assumewehaveafilecalled"vars.php",withsomevariablesdefined:
Then,ifweincludethe"vars.php"file,thevariablescanbeusedinthecallingfile:
Example




Welcometomyhomepage!






Runexample?
PHPincludevs.require

TherequirestatementisalsousedtoincludeafileintothePHPcode.
However,thereisonebigdifferencebetweenincludeandrequire;whenafileisincludedwiththeincludestatementandPHPcannotfindit,thescriptwillcontinuetoexecute:
Example




Welcometomyhomepage!






Runexample?Ifwedothesameexampleusingtherequirestatement,theechostatementwillnotbeexecutedbecausethescriptexecutiondiesaftertherequirestatementreturnedafatalerror:
Example




Welcometomyhomepage!






Runexample?

Userequirewhenthefileisrequiredbytheapplication.

Useincludewhenthefileisnotrequiredandapplicationshouldcontinuewhenfileisnotfound.告诉你了一个方式,但是缺少努力这一环节,那也是白搭。
沙发
发表于 2015-1-18 22:19:44 | 只看该作者
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
爱飞 该用户已被删除
板凳
发表于 2015-1-24 21:50:16 | 只看该作者
至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。
深爱那片海 该用户已被删除
地板
发表于 2015-2-2 14:56:49 | 只看该作者
做为1门年轻的语言,php一直很努力。
老尸 该用户已被删除
5#
发表于 2015-2-7 23:42:15 | 只看该作者
环境搭建好,当你看见你的浏览器输出“it works\\\\\\\"时你一定是喜悦的。在你解决问题的时候,我强烈建议多读php手册。
愤怒的大鸟 该用户已被删除
6#
 楼主| 发表于 2015-2-23 19:48:50 | 只看该作者
作为一个合格的coder 编码的规范是必须,命名方面我推崇“驼峰法”,另外就是自己写的代码最好要带注释,不然时间长了,就算是自己的代码估计看起来都费事,更不用说别人拉。
小妖女 该用户已被删除
7#
发表于 2015-3-7 10:09:44 | 只看该作者
我还是强烈建议自己搭建php环境。因为在搭建的过程中你会遇到一些问题,通过搜索或是看php手册解决问题后,你会更加深刻的理解它们的工作原理,了解到php配置文件中的一些选项设置。
柔情似水 该用户已被删除
8#
发表于 2015-3-14 22:53:24 | 只看该作者
如果你已经到这种程度了,那么你已经可以做我的老师了。其实php也分很多的区域,
透明 该用户已被删除
9#
发表于 2015-3-21 15:41:17 | 只看该作者
写的比较杂,因为我也是个新手,不当至于大家多多指正。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-21 02:32

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表