仓酷云

标题: PHP编程:php中的include和require的区分 [打印本页]

作者: 愤怒的大鸟    时间: 2015-1-16 22:11
标题: PHP编程:php中的include和require的区分
不可能吃饭的时候咬了自己一下舌头就从此不吃饭了不是?放下畏惧,继续努力,咱们是来征服它的,而不是被它征服的,振奋起来吧同志。次要存眷白色标志语句便可。
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?
PHP编程:php中的include和require的区分
登录/注册后可看大图

Userequirewhenthefileisrequiredbytheapplication.

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




欢迎光临 仓酷云 (http://ckuyun.com/) Powered by Discuz! X3.2