PHP腳本的10個(gè)技巧(轉(zhuǎn)自ZDNet) --包含文件
Jun 21, 2016 am 09:12 AM技巧|腳本
包含文件
如果你目前著手的Web站點(diǎn)開(kāi)發(fā)項(xiàng)目規(guī)模較大,那你對(duì)代碼重用必定深有體會(huì),比方說(shuō),不管是HTML還是PHP代碼塊,只要項(xiàng)目足夠大,比如有1000個(gè)Web頁(yè),你即便是一年只修改一次包含版權(quán)信息的頁(yè)腳也會(huì)讓你痛不欲生。
而PHP可以通過(guò)一些函數(shù)幫助你重用代碼,你要具體用到的函數(shù)依賴(lài)于你打算重用的內(nèi)容。
主要的函數(shù)是:
* include() 和 include_once()
* require() 和require_once()
include()函數(shù)包含給定的文件,比如:
include('/home/me/myfile');
包含文件中的任何代碼都將在include() 所在的代碼頁(yè)的變量范圍之內(nèi)執(zhí)行。你可以包含服務(wù)器上的靜態(tài)文件或者包含在其他服務(wù)器上的目標(biāo)文件,方法是組合使用include()和fopen()。
include_once()函數(shù)和include()函數(shù)功能類(lèi)似,只是該函數(shù)會(huì)檢查被包含文件內(nèi)的代碼是否已經(jīng)被目前的腳本所包含過(guò)。如果代碼已經(jīng)被腳本所包含,該函數(shù)就不再包含相應(yīng)的文件了。
require()函數(shù)用給定文件的內(nèi)容取代自身。這一替換過(guò)程發(fā)生在PHP引擎編譯你的代碼而不是執(zhí)行代碼階段,這可和include()不一樣,后者首先被計(jì)算再加到文擋中。require()函數(shù)多用于靜態(tài)元素,而include()函數(shù)則主要用于動(dòng)態(tài)元素。和include_once()類(lèi)似,require_once()函數(shù)會(huì)檢查給定代碼是否早已經(jīng)插入到了文擋內(nèi),如果這樣,給定代碼就不會(huì)被再次插入文檔。
我建議你最好對(duì)諸如版權(quán)、靜態(tài)文本和其他不含變量的元素這類(lèi)信息采用require函數(shù),那些依賴(lài)其他腳本來(lái)實(shí)現(xiàn)自己內(nèi)容的元素也最好采用require函數(shù),例如:
[a lot of content]
// insert copyright
require('/home/me/mycopyright');
?>
另一方面,我經(jīng)常用include()函數(shù)把函數(shù)庫(kù)或類(lèi)似的內(nèi)容放到腳本之外:
// get my function library
include('/home/me/myfunctions');
// do PHP things with my functions ?>
[a lot of content]
你肯定會(huì)問(wèn):“那么,被included或者被required的文件都來(lái)自哪里呢?”答案很簡(jiǎn)單:“你的系統(tǒng)?!辈贿^(guò),有時(shí),有些代碼會(huì)包含了諸如帶有用戶名和密碼的數(shù)據(jù)庫(kù)連接函數(shù)之類(lèi)的安全信息,在這樣的情況下,顯而易見(jiàn),你肯定不希望這些東西也成為文檔的一部分弄得全世界都知道。
你可以把包含文件(included或者required)放在系統(tǒng)的任何地方,只要用戶所能用到的PHP可以訪問(wèn)這些文件即可。你還可以給這些文件以任何你希望給出的文件擴(kuò)展名,或者不設(shè)擴(kuò)展名。
使用include()和require()函數(shù)可以讓W(xué)eb站點(diǎn)上普遍存在或者經(jīng)常變動(dòng)的元素變得更加容易處理。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The key to keep up with HTML standards and best practices is to do it intentionally rather than follow it blindly. First, follow the summary or update logs of official sources such as WHATWG and W3C, understand new tags (such as) and attributes, and use them as references to solve difficult problems; second, subscribe to trusted web development newsletters and blogs, spend 10-15 minutes a week to browse updates, focus on actual use cases rather than just collecting articles; second, use developer tools and linters such as HTMLHint to optimize the code structure through instant feedback; finally, interact with the developer community, share experiences and learn other people's practical skills, so as to continuously improve HTML skills.

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

To embed videos in HTML, use tags and specify the video source and attributes. 1. Use src attributes or elements to define the video path and format; 2. Add basic attributes such as controls, width, height; 3. To be compatible with different browsers, you can list MP4, WebM, Ogg and other formats; 4. Use controls, autoplay, muted, loop, preload and other attributes to control the playback behavior; 5. Use CSS to realize responsive layout to ensure that it is adapted to different screens. Correct combination of structure and attributes can ensure good display and functional support of the video.

To reduce the size of HTML files, you need to clean up redundant code, compress content, and optimize structure. 1. Delete unused tags, comments and extra blanks to reduce volume; 2. Move inline CSS and JavaScript to external files and merge multiple scripts or style blocks; 3. Simplify label syntax without affecting parsing, such as omitting optional closed tags or using short attributes; 4. After cleaning, enable server-side compression technologies such as Gzip or Brotli to further reduce the transmission volume. These steps can significantly improve page loading performance without sacrificing functionality.

HTMLhasevolvedsignificantlysinceitscreationtomeetthegrowingdemandsofwebdevelopersandusers.Initiallyasimplemarkuplanguageforsharingdocuments,ithasundergonemajorupdates,includingHTML2.0,whichintroducedforms;HTML3.x,whichaddedvisualenhancementsandlayout

Adeclarationisaformalstatementthatsomethingistrue,official,orrequired,usedtoclearlydefineorannounceanintent,fact,orrule.Itplaysakeyroleinprogrammingbydefiningvariablesandfunctions,inlegalcontextsbyreportingfactsunderoath,andindailylifebymakingintenti

It is a semantic tag used in HTML5 to define the bottom of the page or content block, usually including copyright information, contact information or navigation links; it can be placed at the bottom of the page or nested in, etc. tags as the end of the block; when using it, you should pay attention to avoid repeated abuse and irrelevant content.

ThetabindexattributecontrolshowelementsreceivefocusviatheTabkey,withthreemainvalues:tabindex="0"addsanelementtothenaturaltaborder,tabindex="-1"allowsprogrammaticfocusonly,andtabindex="n"(positivenumber)setsacustomtabbing
