|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
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.告诉你了一个方式,但是缺少努力这一环节,那也是白搭。 |
|