PHP相關(guān)外文翻譯_第1頁
PHP相關(guān)外文翻譯_第2頁
PHP相關(guān)外文翻譯_第3頁
PHP相關(guān)外文翻譯_第4頁
PHP相關(guān)外文翻譯_第5頁
已閱讀5頁,還剩19頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

中文5915字,3600單詞,17000英文字符本科畢業(yè)設(shè)計(論文)外文參考文獻(xiàn)譯文及原文學(xué)院信息工程學(xué)院專業(yè)信息工程(電子信息工程方向)年級班別學(xué)號學(xué)生姓名指導(dǎo)教師外文參考文獻(xiàn)譯文1為什么選擇PHP錯誤!未定義書簽2如果你是編程新手錯誤!未定義書簽3寫一個基本的PHPS序錯誤!未定義書簽4編程語法錯誤!未定義書簽5嵌入式語言如何工作錯誤!未定義書簽6服務(wù)端和客戶端腳本錯誤!未定義書簽7運行你的程序錯誤!未定義書簽外文參考文獻(xiàn)原文WhyPHP?錯誤!未定義書簽IfYouAreNewtoProgramming錯誤!未定義書簽WritingaBasicPHPProgram錯誤!未定義書簽ProgrammingSyntax錯誤!未定義書簽HowEmbeddedProgrammingWorks錯誤!未定義書簽Server-sideVersusClient-sideScripting錯誤!未定義書簽RunningYourNewProgram錯誤!未定義書簽1為什么選擇PHP對于Web編程來說,PHP是一個很好的選擇。它較其它語言(包括其它面向Web的語言)有許多優(yōu)點。為了得到一個清晰的理解(和常見的面向Web的語言相比),讓我們將它們比較一下。ASP是微軟的網(wǎng)絡(luò)開發(fā)環(huán)境(它本身不是一門開發(fā)語言,因為它允許程序員在ASP中選擇其它語言進(jìn)行開發(fā),如VBScript或JScript。)ASP雖然簡單,但它太過于簡單了,以至于不能使用更復(fù)雜的邏輯和算法。除了ASP的過分簡單,很多公司發(fā)現(xiàn)很難在微軟的ASP許可證上節(jié)約成本。即使不考慮硬件成本,微軟的Web服務(wù)器就要數(shù)千美元,而基于UNIX操作系統(tǒng)的、可運行PHP的Web服務(wù)器則是免費的。另一種Web使用的知名語言是SunMicrosystems公司的Java。Java是平臺獨立的語言(在一個系統(tǒng)上用Java開發(fā)的程序可以不經(jīng)過任何修改,就可以運行在其它系統(tǒng)上)。盡管Java的確有它的優(yōu)點,但它在開發(fā)時間、開發(fā)成本和執(zhí)行速度上卻有明顯的劣勢。Java開發(fā)是耗時的,因為Java工程要遵循嚴(yán)格的規(guī)則(Java強制的),這需要進(jìn)行大量的規(guī)劃。另外,Java開發(fā)周期長,費用成本高,Java程序員的待遇也較高。因此Java程序開發(fā)的成本遠(yuǎn)遠(yuǎn)高于其它語言開發(fā)的成本。通過我們的比較可以看出,即使項目開發(fā)結(jié)束后,以Java語言寫的程序在運行時所花費的時間比其它語言的時間還要長??偟膩碚f,和Java相比,PHP的表現(xiàn)很出色。一個相似的工程以PHP語言開發(fā),要比用Java開發(fā)節(jié)省兩三倍的時間。最重要的是,雖然最終的程序都可以運行多種平臺上(如Javaj),而PHP的程序則運行的更快。另一種開發(fā)Web程序的常用語言是Perl。Perl與PHP一樣,是一個可運行在多個平臺上的開源項目。事實上,Perl的歷史遠(yuǎn)遠(yuǎn)要比PHP長。在PHP之前,Perl被認(rèn)為是最好的Web開發(fā)語言。然而,在過去的幾年中,在Web開發(fā)領(lǐng)域,PHP贏得了比Perl更好的口碑。因為PHP語言本身集成了大量的功能模塊,而要在Perl中使用這些功能,你就不得不單獨下載許多額外的功能模塊。這樣,當(dāng)程序從一個系統(tǒng)轉(zhuǎn)移到另一個系統(tǒng)時,就會容易產(chǎn)生問題,因為Perl不得不從自己的CPAN上下載相應(yīng)的功能模塊。最后要比較的語言是CC語言已經(jīng)存在很長的時間了;它已被用在各種各樣的計算機中,從大型主機到個人PC都可見到它的身影。如果你了解C語言的話,你就會知道用C語言開發(fā)Web程序會存在一些問題。為了用C語言開發(fā)一個Web程序,在你著手構(gòu)思你的實際任務(wù)時,你將不得不著手實現(xiàn)所有Web程序本身就應(yīng)該具備的功能(如采集HTML表單提交的數(shù)據(jù))。而PHP語言提供了所有的Web常用或者并不是很常用的功能,可以允許程序員將更多的精力放在實際的任務(wù)中。就其它的Web開發(fā)語言與PHP語言之間的比較而言,它們之間還存在很多不同。在Internet上將PHP與Java,Perl,ASP等語言進(jìn)行比較的文章也很多。但是一旦你擁有了PHP的編程經(jīng)驗,你可能就會發(fā)現(xiàn),在選擇開發(fā)語言時,你會努力的說服你的客戶或老板,允許你使用PHP來進(jìn)行開發(fā),而不是選擇其它的語言。如果出現(xiàn)問題,你可以通過搜索Internet找到大量基于這些問題的有用信息。PHP的使用是沒有限制的。早期的版本使用的是單獨的跟蹤程序。而隨著時間的推移,這個簡單的跟蹤程序已經(jīng)被包含在語言的內(nèi)部了。PHP的主要用途,當(dāng)然不只是用來跟蹤的了;它還可以做許多其它的事情。為了讓你理解PHP可以做什么,下面介紹了一些PHP的常見用途:?反饋表單?購物車和其它類型的電子商務(wù)系統(tǒng)?用戶注冊,訪問控制,以及在線訂閱服務(wù)?客戶登記?討論和留言板2如果你是編程新手如果你是一個從來沒有寫過程序的新手,這個想法可能有點嚇人。大多數(shù)程序員可能會告訴你(如果他們愿意承認(rèn)),他們剛開始學(xué)習(xí)編程的時候,也有點無從下手。然而,編程的過程根本不是那么難,相反卻很享受,因為你不需要有多高的智商來適應(yīng)它。當(dāng)你寫程序的時候,你的主要目的就是把你的想法翻譯成計算機能夠識別的語言。例如,如果你是教人如何做漢堡包,你會首先描述將牛肉做成牛肉餅的過程。然后,你會告訴他如何把漢堡包放在烤架上,烤多長時間,最后,如何把它們拿下來。當(dāng)然,僅僅因為你能描述制作漢堡包的過程,并不意味著PHP能夠在任何時間、任何地點為你做任何事情。重點是,如果你能像我剛才描述制作漢堡包的過程那樣,描述你的想法及思路,你就可以寫程序?qū)懗绦虻倪^程就是告訴PHP如何做事情的過程。當(dāng)你讀完本文的時候,你就會明白圍繞在PHP程序背后的所有概念,這些概念就像描述制作漢堡包的過程一樣。對于本文,你讀的越透,你明白的概念就越多,你向PHP描述的任務(wù)就越準(zhǔn)確。因此,如果你很快學(xué)會用PHP程序來實現(xiàn)你的需求和想法,你就不會再感覺到煩惱了。整體說來,有些編程問題可能是非常復(fù)雜的。例如,創(chuàng)建一個購物車,那并不是一個簡單的事任務(wù)。然而,你可以將這個大任務(wù)分成幾個小任務(wù)。這些小任務(wù)可能僅僅包括添加和刪除的操作,而添加和刪除的操作還可以被分解成更小的任務(wù)。你可以看到,不管任何任務(wù),無論它有多復(fù)雜,只要可以分解,分解后的任務(wù)簡單到不需要再對它進(jìn)行分解了,你就可以實現(xiàn)這個任務(wù)。當(dāng)你創(chuàng)建更復(fù)雜的程序時,你就會對這個過程有更加細(xì)致的體會了。3寫一個基本的PHP程序在我們開始寫一個實際程序的時候,要看看創(chuàng)建程序的步驟。這些步驟并不復(fù)雜;實際上,這個過程和你創(chuàng)建一個HTML頁面,并將它同步到你的服務(wù)器上的過程基本一致。和創(chuàng)建一個HTML頁面不同,創(chuàng)建一個PHP程序,你要面對的是文件中的代碼,而不是那些所見即所得(WYSIWYG)的內(nèi)容。如果你要利用這些所見即所得的編輯器開發(fā)程序(如微軟的FrontPag^MacromediaDream-Weaver微軟Word等),你可能還要花費一些時間來研究代碼。但令人興奮的是,沒有人也沒有任何理由要求你,不能使用所見即所得的編輯器來編輯程序。然而,你可能會失望的發(fā)現(xiàn),許多所見即所得的編輯器會修改或刪除PHP文件中的代碼。積于這個原因,分析你的編輯器是如何處理PHP代碼的,這很重要。如果你想用你的編輯器來進(jìn)行這種測試,要先創(chuàng)建一個新文件,保存,后綴名為.php。然后,切換到編輯器的源碼視圖,或在一個獨立的其它編輯器中打開這個文件(如記事本等),輸入接下來本文要引用的代碼,并確保沒有任何錯誤。當(dāng)你輸入完成后,保存文件并切換回WYSIWYG編輯器視圖。如果你看到你的PHP代碼,不用管它,繼續(xù)鍵入幾行文字。如果你想添加一些Web頁面中常用的元素,如表格和圖片等,就請繼續(xù)插入這些內(nèi)容,保存,并關(guān)閉WYSIWYG編輯器?,F(xiàn)在,在記事本中打開該文件,看看你的PHP代碼是否發(fā)生了變化,包括代碼格式上的改變,特殊字符的轉(zhuǎn)換(如<<)以及完全被刪除的代碼。你可能會發(fā)現(xiàn)PHP代碼被以某種方式改變了。因為PHP代碼對一些WYSIWYG編輯器導(dǎo)致的變化是敏感的,如果你在寫PHP代碼的時候,使用WYSIWYG編輯器,這就很難讓人理解了。PHP社區(qū)不會因為這個原因,而建議你不要使用WYSIWYG編輯器,因為使用這樣的編輯器有時還是可以加快程序的開發(fā)速度的?,F(xiàn)在,當(dāng)你讀代碼或者測試本文中的程序時,請試著習(xí)慣使用這些純文本編輯器吧。當(dāng)你習(xí)慣使用純文本編輯器的時候,再使用其它的編輯器,那就容易上手的多了。到那時,你就可以識別出編輯器自身生成的代碼,也就能更容易的找出提高你工作效率的方法了。不管你現(xiàn)在處理PHP代碼的編輯器是什么,如果你正在用的是WYSIWYG編輯器,我建議你使用如記事本這樣的純文本編輯器,或者一些帶有語法加亮功能的純文本編輯器。這些編輯器可以保證你的代碼就像剛輸入時的一樣(WYSIWYG編輯器傾向于以它自己的規(guī)則來格式化你的代碼,而這種功能對于編輯PHP代碼來說并不需要)。即使你的編輯器沒有改變你的PHP代碼,但如果它不是一個嚴(yán)格基于文本的編輯器,仍然會在隨后的運行中可能會出現(xiàn)問題。這是創(chuàng)建和瀏覽HTML文件的一般過程:1.創(chuàng)建HTML文件(添加文本,表格,圖像,或聲音等)。2,保存HTML文件,命名為filename.htmL.用FTP程序把你的文件上傳到Web服務(wù)器。.用你的瀏覽器訪問Web服務(wù)器上的文件(如,http:///filename.html)。創(chuàng)建PHP程序的過程大致與創(chuàng)建HTML文件的過程相同:.創(chuàng)建HTML文件(包含文字,表格,圖像,或聲音),并在適當(dāng)?shù)奈恢貌迦隤HP代碼。.保存PHP文件,命名為filename.php。.使用FTP程序上傳你的文件到Web服務(wù)器。.用你的瀏覽器訪問Web服務(wù)器上的文件(如http://Zfilename.php)??梢?,創(chuàng)建PHP程序和創(chuàng)建常規(guī)HTML程序的過程沒有多大不同。在了解了創(chuàng)建PHP程序的大致過程后,請看一下你的第一個PHP程序??催^代碼你將了解,如何將PHP文件與普通的HTML文件相區(qū)分,如何把它上傳到你的Web服務(wù)器上,以及在瀏覽器中頁面看起來應(yīng)該是什么樣的。<!--File:filename.php--><html><head><title>PHP<Ztitle><Zhead><bodybgcolor="white"text="black”><h4>PHP<Zh4><?phpZ*Displayatextmessage*Zecho“Hello,world!ThisismyfirstPHPprogram.”;?><Zbody><Zhtml>這個文件看起來和一個普通的HTML文件很像。注意,文件中包含了典型的HTML標(biāo)簽。事實上,如果你忽略<?php和?>標(biāo)簽間的內(nèi)容,你也可以把該文件重新命名為.html的后綴名。然而,這個文件的確包含了PHP代碼,所以它必須被命名為.php后綴名。PHP代碼在兩個PHP標(biāo)簽之間((<?php和?>)),如圖1.1所示。PHP標(biāo)簽之間的命令是“echo”(PHP的關(guān)鍵字,意思是顯示文本”)隨后跟的是要顯示的文本。結(jié)果很快被顯示出來,“echo”后面的文本看起來就像是包含在HTML文件本身一樣,似乎就沒有PHP代碼存在過。<!--Fila:ch9l.php<html>HTML<hMd><tltl0>PHP/titL&x/h?ad>bocolor='whiTejtexT-=_tiack'>HTML^h4>PHPvti4A<?phpF*DisplayaTextm0ssag@*?cbo,Holl。,<?phpF*DisplayaTextm0ssag@*?cbo,Holl。,world1ThisismyfirstPHPprogram.";STARTTAGCODE/COMMANDSPHPENDTAGHTML<body>HTML圖1.1:這幅圖顯示了一個基本的PHP程序的不同部分。在看輸出的結(jié)果之前,我們先把這個文件上傳到Web服務(wù)器上并且運行。按照上面寫PHP程序的過程,將它保存為PHP文件(.php后綴名),并上傳到你的Web服務(wù)器。一旦你的程序被上傳到您的Web服務(wù)器,在你的瀏覽器中輸入該文件地址。你應(yīng)該得到一個返回的頁面,它看起來應(yīng)該非常類似如下的截圖。圖1.2:當(dāng)你訪問你的程序時應(yīng)該看到這樣的頁面4編程語法當(dāng)你訪問剛剛上傳的PHP程序時,該PHP程序在返回瀏覽器之前是經(jīng)歷了一個復(fù)雜的過程的。PHP文件中的相關(guān)命令就是在這個過程中被執(zhí)行的;在這個例子中,是一個單獨的echo語句。圖1.3顯示當(dāng)PHP文件發(fā)送請求時歷經(jīng)的過程。圖1.3:不像HTML文件,PHP文件在被返回瀏覽器時執(zhí)行了文件中包含的PHP命令。PHP解釋器(或解析器)程序,正如上圖中看到的那樣。它讀取PHP程序并執(zhí)行它所能理解的命令。(如果PHP碰巧遇到一個它不能理解的命令,它會停止繼續(xù)解析該文件,并返回一個錯誤消息給瀏覽器。)每當(dāng)PHP在給Web服務(wù)器發(fā)送請求時,該PHP解釋器在給瀏覽器反饋信息前,必須先處理PHP文件中的命令。因為PHP的每次運行,都必須調(diào)用相關(guān)的例程,以便執(zhí)行這些命令。這和編譯型語言有所不同,如C或C++,它們只被解釋一次;C語言程序,直接被翻譯成機器碼(機器碼直接由CPU處理)。PHP不得不解釋程序中包含的命令,這些命令還在必須以PHP能夠識別的形式給出。例如,如果有人走向你,用德語詢問時間,你可能不知道他在說什么(除非你懂得德語或者這個人指著他的手腕)。同樣地,如果我走到你面前說,用英文語美味問你,現(xiàn)在幾點了?你可能也不知道我在說什么。PHP也有類似的限制。語句(命令的集合,由特定字符組成,PHP能夠識別的句子)必須以正確的命令格式給出。例如,你要在PHP中顯示一條文本,你必須使用PHP能夠識別的命令,如"echd'。同時,你必須把你的單詞,以正確的順序,用英語講給別人。還要格式化要傳遞給PHP執(zhí)行的語句,以便PHP解釋器能夠識別并執(zhí)行。語法,就是將PHP能夠理解并執(zhí)行的句子組織到一起的過程中要遵循的規(guī)則,這就是PHP的開始和結(jié)束標(biāo)簽。PHP只解析在PHP標(biāo)簽之間的代碼。而不在這其中的代碼只是作為HTML頁面的一部分返回,正如先前看到例子一樣。如下這個例子,該語句不能執(zhí)行,即使該命令是PHP語言的命令:echo“Thiswon'twork.”該語句不會執(zhí)行,因為它沒有遵循所有的語句必須以分號結(jié)尾的基本語法規(guī)則。一些特殊的語句不需要有分號,但不是很多(如do語句。)注意,PHP語句必須以分號結(jié)束。下面的語句能夠正確執(zhí)行并輸出:echo“Thisworks!”;你可能注意到了,即使兩個語句在同一行,但語句問并沒有分號分隔,PHP并不會報錯。這是PHP的一個特性,它使得插入一個單獨的echo語句變得更加容易。如果你想在第一個echo語句運行后看到返回的錯誤提示,那么將這兩個語句輸出到不同的行,看起來就像這個樣子:echo“Thiswon'twork.”echo“Thiswon'twork.”代碼不會運行,PHP將返回一個錯誤信息,因為沒有分號來分隔這兩個語句。5嵌入式語言如何工作通過前面的講解,我們知道PHP代碼必須封裝在<?PHP和?>這兩個PHP標(biāo)簽之間。在同一文件中,使用標(biāo)簽來分隔PHP代碼和HTML代碼,這就允許將PHP代碼要處理的信息和HTML要顯示的信息直接混合在一起。這使得PHP成為一種嵌入式編程語言,因為PHP代碼被直接嵌入在HTML代碼中了。這個概念是相對較新的:在PHP語言出現(xiàn)之間,并沒有程序要像HTML這樣復(fù)雜的格式化顯示的需求。顯示在屏幕上的信息通常只是字母,數(shù)字,和空格,沒有顏色,大小或其它的格式標(biāo)記。自從PHP作為Web編程語言出現(xiàn)以來,它便趨向于在HTML中使用,這大大增加了要返回給瀏覽器的信息量。PHP不僅要返回信息給用戶,而且也需要正確的格式化這些信息。為了使信息和標(biāo)簽?zāi)軌蚝唵位旌?,PHP代碼被直接嵌入在HTML頁面中。開篇的例子比較清楚的演示了這一概念;程序里大部分是常規(guī)的HTML代碼,但PHP也會插入一些適當(dāng)?shù)男畔⒌巾撁嬷?。嵌入式編程,將使程序員的工作更為便捷;你可以在任何你想要插入代碼的地方插入PHP代碼,剩下的地方就寫常規(guī)的HTML代碼了。然而,別忘了用PHP標(biāo)簽封裝你的PHP代碼,否則你的代碼將不能被解析,而是會被直接顯示在HTML頁面上。下面的程序演示了一個嵌入式編程的例子:<?php/*File:hello_world.php-displays“Hello,World!”*/?><html><head><title>Hello,World!</title></head><bodybgcolor="white"text="black”>Hello,<?php//Send“World!”tothevisitor?sbrowserecho“World!”;?></body></html>當(dāng)Web服務(wù)器訪問這個文件的時候,PHP解釋器將會從頭到尾的逐行執(zhí)行文件中的命令。因此,PHP標(biāo)簽間處理的信息就隨著echo語句一起返回給瀏覽器了。Web瀏覽器接收到的內(nèi)容和如下代碼所示效果相同:<html><head><title>Hello,World!</title></head><bodybgcolor="white"text="black''>Hello,World!</body></html>然后瀏覽器就可以像顯示其它HTML文件一樣顯示“HelloWorld!”信息了。6服務(wù)端和客戶端腳本就像先前解釋的一樣,在信息返回瀏覽器之前,PHP代碼要在Web服務(wù)器端執(zhí)行,這被稱為服務(wù)器端處理。大多數(shù)的Web編程都是以這種形式存在的:像ASP、PHP、Perl、C等。然而,還有一些語言是在瀏覽器接收到返回的頁面時進(jìn)行處理的。這就是所謂的客戶端處理。最常見的例子就是JavaScript。這可能會導(dǎo)致邏輯上的一個有趣的問題。下面的示例演示了這種情況:<scriptlanguage='JavaScript">if(testCondition()){<?phpecho“<b>Theconditionwastrue!</b>”;?>}else{<?phpecho“<b>Theconditionwasnottrue.</b>”;?>}</script>多數(shù)情況下,這么寫代碼的程序員通常希望只有一個echo語句被執(zhí)行。然而,這兩句都將執(zhí)行,頁面將停留在生成錯誤的JavaScript頁面(因為echo語句中的信息不是有效的JavaScript代碼)。如果你還不是很清楚,繼續(xù)看下面的例子;如下的代碼會讓你更加明確。這段代碼是從以前的代碼片段中摘錄下來的,注意JavaScript是原封不動的,但PHP代碼已經(jīng)被調(diào)用了。PHP完全忽略了JavaScript代碼:<scriptlanguage='JavaScript'>if(testCondition()){<b>Theconditionwastrue!</b>}else{<b>Theconditionwasnottrue.</b>}</script>正如你所看到的,這會導(dǎo)致JavaScript執(zhí)行時的錯誤。將PHP和JavaScript代碼混合使用要小心:可以這樣做,但是要注意,PHP將永遠(yuǎn)無視JavaScript的存在。為了成功地將二者結(jié)合起來,用PHP輸出JavaScript代碼是非常必要的。下面的例子就是這么做的:<scriptlanguage='JavaScript'>if(testCondition()){<?phpecho“document.whte('<b>Theconditionwastrue!</b>,);";?>}else{<?phpecho“document.whte('<b>Theconditionwasnottrue.</b>,);";?>}</script>7運行你的程序按照本文開始介紹的過程,嘗試運行這個程序。你將在你的瀏覽器里看到HelloWorld!WhyPHP?PHPisanexcellentchoiceforWebprogramming.Ithasmanyadvantagesoverotherlanguages,includingotherWeb-orientedlanguages.TogetaverygeneralunderstandingofhowthecommonWebprogramminglanguagescompare,let令comparethem.ASPisMicrosoftaWebprogrammingenvironment.(Itsnotalanguageitselfbecauseitallowstheprogrammertochoosefromafewactuallanguages,suchasVBScriptorJScript.)ASPissimple,buttoosimpleforprogramsthatusecomplexlogicoralgorithms.BesidesASPsover-simplicity,manycompaniesfindithardtobudgetfortheexpenseofMicrosoftlicenses.Withoutevenconsideringhardwarecosts,aMicrosoftservercouldcostthousandsofdollarsinlicensing,whereasacomparableUnix-basedoperatingsystemrunningPHPcouldbefree.AnotherlanguagewellknownforitsuseontheWebisSunMicrosystems?Java.Javaispraisedforbeingplatform-independent(aprogramwritteninJavacanberunonvirtuallyanycomputerwithouthavingtomakeanymodificationstotheprogram).AlthoughJavadoeshaveitsadvantages,ithasseriousdownsidesindevelopmenttime,developmentcost,andexecutionspeed.Javadevelopmentistime-consumingbecauseprojectsinJavamustfollowstrictrules(imposedbyJava)thatrequireextensiveplanning.Inadditiontohighdevelopmenttime,thecostisalsohighbecauseJavadevelopersareexpensivetohire.Thecostisthereforepotentiallymuchhigherthanitwouldbeiftheprojectweredoneinanotherlanguage.Evenaftertheprojectisbuilt,aprogramwritteninJavatakeslongertorunthanonewritteninoneoftheotherlanguagestowhichwe?recomparing.Overall,whencomparedtoJava,PHPcomesoutwithflyingcolors.ItisnotunheardofforaJavaprojecttotaketwoorthreetimesthetimetodevelopcomparedtoasimilarprojectinPHP.Ontopofthat,thefinalprogramrunsonawidearrayofplatforms(likeJava),exceptthePHPprogramrunsfaster.AnotherlanguagecommonlyusedforwritingWebprogramsisPerl.Perl,likePHP,isanopen-sourceprojectdevelopedtorunonmanyplatforms.Infact,PerlhasbeenaroundlongerthanPHP.BeforePHP,PerlwasgenerallyacceptedasthebestWebprogramminglanguage.However,duringthepastfewyears,PHPhasearnedareputationforbeingbetterthanPerlforWebprogrammingbecausePHPprovidesavastnumberoffeaturesaspartofPHPitself,whereasyouwouldhavetodownloadseparatemodulestogetthesamefunctionalityinPerl.ThisleadstoproblemswhenprogramsaretransferredfromonesystemtoanotherbecausethemoduleshavetobedownloadedfromPerlaexhaustive(andconfusing)modulearchiveknownasCPAN.ThelastlanguagetocomparePHPtoisC.Chasbeenaroundforalongtime;ithasbeenusedinavarietyofcomputers,frommainframestoconsumerPCs.TheproblemscreatingaWebprograminCareobviousifyouknowC.TodevelopaWebprograminC,youhavetodevelopallofthebasicfunctionalityofWebprogramming(suchascollectingthedatafromHTMLforms)beforeyoucanevenbegintothinkabouttheactualtaskathand.SincePHPprovidesforallthecommon(andmanyuncommon)Webprogrammingtasks,writingsuchaprograminPHPallowstheprogrammertogetstraighttothepoint.YoucouldwritevolumesonPHPsadvantagesoverotherprogramminglanguageswhenitcomestoWebprogramming.Therearemany,manyarticlesontheInternetcomparingPHPtoJava,Perl,ASP,andothers.OnceyouReearnedsomeexperienceprogramminginPHP,youmightfindyourselftryingtoconvinceyourclientoremployertoallowyoutouseitinsteadofanotherlanguage.Ifthatproblemarises,youshouldfindplentyofhelpfulinformationbydoingaWebsearch.PHPhasanunlimitednumberofuses.Theoriginalversionwasusedsolelytotrackwhowasviewingthecreator&resume.Overtime,however,thatsimpletrackingprogramevolvedintoalanguageofitsown.PHP?SprimaryusecertainlyisnZtotrackresumesanymore;ithasgrowntobeabletodothatandjustaboutanythingelse.TogiveyouabetterideaofwhatPHPcando,herearesomeofitscommonuses:?Feedbackforms?Shoppingcartsandothertypesofe-commercesystems?Userregistration,accesscontrol,andmanagementforonlinesubscriptionservices?Guestbooks?DiscussionandmessageboardsIfYouAreNewtoProgrammingIfyou?veneverwrittenacomputerprogrambefore,thewholeideamaybequiteintimidating.Mostprogrammerswillprobablytellyou(iftheyaren?embarrassedtoadmitit)thattheywereintimidatedwhentheybegan.However,theprogrammingprocessisntallthatdifficultand,contrarytopopularbelief,youdonZhavetohaveanextremelyhighIQtobegoodatit.Whenyouwriteaprogram,yourmaingoalistotranslateyourideaintoalanguagethatthecomputercanunderstand.Forexample,ifyouwereteachingapersonhowtocookhamburgers,youwouldfirstdescribetheprocessofformingthegroundbeefintopatties.Then,youwouldtellthepersonhowtoputtheburgersonthegrill,howlongtoleavethemthere,andfinallyhowtoremovethem.Ofcourse,justbecauseyoucandescribetheprocessofmakinghamburgersdoesrPtmeanPHPisgoingtobecookinganythingforyouanytimesoon.Thepointis,ifyoucandescribeaprocesslikeIjustdescribedmakinghamburgers,youcanwriteaprogram.WritingaPHPprogramissimplytheprocessofdescribingtoPHPhowtodosomething.Bythetimeyou?vefinishedreadingthisarticle,youwillunderstandalltheconceptsbehindwritingaPHPprogram.Thoseconceptsarelikethewordsandsentencesusedtodescribehamburgers.Themoreyoureadthisarticle,themore“words“youwillunderstand,andthebetteryouwillbeableto"describeyourtasktoPHP.Thus,youwilllearntowritePHPprogramstosuitwhateverneedorideayouhave,andsoonitwonZbeanymoreintimidatingthantellingsomeonehowtocookhamburgers.Someprogrammingproblemsmightbeverycomplexwhenexaminedasawhole.Forexample,creatingashoppingcartisdefinitelynotasimpletask.However,ashoppingcartcanbebrokenintoafewsmallertasks.Thosetasksmightincludeaddingandremovingitems,whicharebothtasksthatcanbreakintoevensmallertasks.Youwillfindthatanytask,nomatterhowcomplex,canbebrokenintosmalleronesuntileachtaskissimpleenoughthatbreakingitdownfurtherisunnecessary.ThisprocessisexplainedinmoredetailwhenyoubegincreatingprogramswithmorecomplexityWritingaBasicPHPProgramBeforewegetintoanactualprogram,letstakealookatthestepswelltaketocreateone.Thestepsarentcomplicated;infact,they?rebasicallythesameasthestepsyouusewhencreatinganHTMLpageandpublishingittoyourserver.UnlikecreatinganHTMLpage,creatingaPHPprogramrequiresthatyouactuallyworkwiththesourcecodeofthefileasopposedtoa“whatyouseeiswhatyouget”(WYSIWYG)approach.Ifyou?reusedtousingaWYSIWYGprogram(suchasMicrosoftFrontPage,MacromediaDream-Weaver,orMicrosoftWord),itmaytakeyousometimetogetusedtolookingatthesourcecode.ThegoodnewsisthereRnoreasonthatyoucan?continuetouseaWYSIWYGeditortocreateanHTMLdesignforyourprogram.However,youmaybedisappointedtofindthatmanyWYSIWYGeditorsmangleorevendeletevitalPHPcodefromyourfiles.Forthisreason,itisimportanttofindouthowyourparticulareditorhandlesPHPcode.IfyouwanttotestyourWYSIWYGtoseehowithandlesPHPcode,createanewfile,namingitwitha.phpextension.Then,switchtoyoureditor?ssourcevieworopenthefileinaseparateprogram,suchasNotepadandentertheprogramshowninthefirstexamplelaterinthechapter,makingsurenottomakeanymistakes.Whenyou^efinished,savethefileandswitchbacktotheWYSIWYGeditor.IfyouseeyourPHPcode,workarounditandtypeafewlinesoftext.Ifyouwant,addsomecommonelementsthatyouincludeinyourWebpages,suchastablesandimages.Savethefileagainandclosealltheopeneditors.Now,openthefileinNotepadandlookatthePHPcode.Lookforanychanges,includingchangesinthewaythecodeisformatted,specialcharactersthathavebeenconvertedintocodes(suchas<to<),andcodethathasbeencompletelyremoved.YouwillprobablyfindthatthePHPcodehasbeenchangedinsomeway.BecausePHPissensitivetosomeofthechangesaWYSIWYGeditormightmake,it?salmostimpossibletouseaWYSIWYGeditoronceyou?/estartedaddingPHPcode.ThePHPcommunitywon?tellyouthatusingaWYSIWYGeditorisasignofweakness;doingsocanspeedthingsupalotsometimes.Fornow,tryusingaplain-texteditorwhenyou^ereadingandexperimentingwiththeexamplesinthisarticle.Whenyou/ecomfortablewiththat,feelfreetotryitwithwhatevereditoryouwant.Bythattime,you?lbeabletorecognizecodethattheeditorhasmangled,andyou?lhaveaneasiertimefindingwhatworksbestforyou.RegardlessofhowyourcurrenteditorhandlesPHPcode,ifyouareusingaWYSIWYGeditor,IsuggestthatyouuseaneditorsuchasNotepadoroneofthemanyfreesyntax-highlightingeditorsoutthere.Usingoneoftheseprogramswillensurethatyourcodestaysjustasyoutypedit(WYSIWYGeditorstendtoreformatthingsastheyseefit,whichisn?tdesirablewhencodingPHP).Evenifyoureditorpassedthetest,ifi禽notastrictlytextbased(notWYSIWYG)editor,youmightfindyourselfrunningintoproblemslater.HereistheprocessyoumightuseincreatingandviewinganHTMLfile:CreateyourHTMLfile(addtext,tables,images,orsounds).SaveyourHTMLfileasfilename.html.UseanFTPprogramtouploadyourfiletotheWebserver.PointyourbrowsertotheaddressofthefileonyourWebserver(forexample,/filename.html).TheprocessyouwouldusetocreateaPHPprogramismuchthesame:CreateyourHTMLfile(containingtext,tables,images,orsounds)andinsertPHPcodewheredesired.SaveyourPHPfileasfilename.php.UseanFTPprogramtouploadyourfiletotheWebserver.PointyourbrowsertotheaddressofthefileonyourWebserver(suchas/filename.php).TheprocessofcreatingaPHPprogramisntmuchdifferentfromtheprocessyoufollowtocreatearegularHTMLpage.Nowthatyou?veseentheoverallprocess,let?stakealookatourfirstPHPprogram.Afterreadingthefollowingexample,you?lllearnwhatseparatesitfromanormalHTMLfile,howtouploadittoyourWebserver,andwhatthepageshouldlooklikeviewedinyourbrowser.<!--File:filename.php--><html><head><title>PHP</title></head><bodybgcolor="white"text="black”><h4>PHP</h4><?php/*Displayatextmessage*/echo“Hello,world!ThisismyfirstPHPprogram.”;?></body></html>ThisfilelooksalotlikearegularHTMLfile.NoticethatthefilehasHTMLtagstypicalofthoseyouwouldfindinanyHTMLfile.Infact,ifyoudisregardeverythingbetweenthe<?phpand?>tags,youmightaswellrenamethisfilewithan.htmlextension.However,thisfiledoescontainPHPcode,soitmustbenamedwitha.phpextension.ThePHPcodeliesbetweenthePHPtags(<?phpand?>)asshowninFigure1.1.ThecommandbetweenthePHPtagsisecho(PHPwordfor“addthefollowingtexttothepage")followedbythetexttodisplay.Theoutput,whichwillbeshownsoon,looksjustasifthetextafterechohadbeeninanHTMLfileitselfandnoPHPcodeeverexisted.Figure1.1:ThisdiagramshowsthedifferentpartsofabasicPHPprogram.Beforewelookattheoutput,letsuploadthisfiletoaWebserverandrunit.Followtheprocessoutlinedpreviouslytowritetheprogram,saveitasaPHPfile(witha.phpextension),anduploadittoyourWebserver.OnceyourprogramisuploadedtoyourWebserver,typeitsaddressintoyourbrowser.YoushouldgetapagebackthatlooksverysimilartothescreenshotinFigure1.2.□5HId*仲frsmsg.b加--■?3£電!京wch?Ffl*wte3Hml1各占"■.二件小品忖,,F(xiàn)h?喝if寸中LTpPHfHrL:w-eriiThjtmy5rrt>HT*pf'?ErinFigure1.2:Thisiswhatyoushouldseeinyourbrowserwhenyougototheaddressofyournewprogram.4ProgrammingSyntaxWhenyouaccessedtheprogramyoujustuploadedwithyourbrowser,thePHPprogramwentthroughaprocessbeforeitwasreturnedtothebrowser.TheprocessperformedthePHPcommandswithinthefile;inthiscase,thatwasasingleechostatement.Figure1.3showswhathappenswhenarequestismadeforaPHPfile.Figure1.3:UnlikeHTMLfiles,PHPfilesareroutedthroughaspecialprocessthatperformsthePHPcommandswithinthefilebeforeitisreturned.ThePHPinterpreter(orparser)istheprogramthatperformstheprocessingmentionedpreviously.ItreadsthePHPprogramfileandexecutesthecommandsitunderstands.(IfPHPhappenstofindacommanditdoesrPtunderstand,itstopsparsingthefileandsendsanerrormessagebacktothebrowser.)EverytimearequestforaparticularPHPfileismadetoaWebserver,thePHPinterpretermustprocessthefilepriortoreturninganythingtothebrowser.BecausePHPmustinterpretaPHPprogrameverytimethatprogramruns,itisknownasascriptinglanguage.Thisisquitedifferentfromacompiledlanguage,suchasCorC++,whichisonlyinterpretedfromahuman-readableformonce;aCprogramissimplytranslatedintomachinecode(codethatisprocesseddirectlybythecomputerprocessor).SincePHPhastointerpretthecommandsincludedwithinaprogram,thecommandsmustbegiveninsuchawaythatPHPunderstandsthem.Forexample,ifsomeonewalkeduptoyouandaskedyouinGermanforthetime,youprobablywouldn2knowwhathewastalkingabout(unlessyouknowGermanorthepersonpointedtohiswrist).Likewise,ifIwalkeduptoyouandsaid,“Istimeitwhat?”youprobablywouldnZknowwhatIwastalkingabout,eventhoughIusedEnglishwords.PHPhassimilarlimitations.Astatement(theconstructionofcommandsandcertaincharacterstomakeasentencethatPHPwillunderstand)mustbegivenusingthecorrectcommands.Forexample,thecommandshowtexthasnomeaninginPHP;youmustinsteaduseacommandPHPrecognizes,suchasecho.Also,justasyoumustputyourwordsinthecorrectordertotalktoanotherpersoninEnglish,youmustformatthestatementsyougivePHPsothattheyfollowtheformatPHPexpects.Syntax,then,istheprocessofputtingtogetherstatementsthatPHPwillbeabletointerpretandcarryout.ExamplesofthisarePHPRopeningandclosingtags.PHPonlyparsescodethatisbetweenPHPtags.AnythingelseinthefileisreturnedaspartoftheHTMLpage,justasseenearlierinthefirstexample.Here?sanotherexample.Thefollowingstatementdoesnotwork,eventhoughthecommandispartofPHPPslanguage:echo“Thiswon?twork.”Thestatementwon?workbecauseitdoesrPtfollowabasicsyntaxrulethatrequiresallstatementstobeterminatedwithasemicolon.Somespecialstatementsmusthavethesemicolonleftout,butnotmany.(Theonesthatdowillbepointedoutaswecometothem.)Fornow,justrememberthatstatementsshouldendwithasemicolon.Thefollowingstatementisacorrectedversionoftheprecedinglineofcode:echo“Thisworks!”;Youmaynoticethatleavingthesemicolonoffasinglestatementdoesn2causePHPtodisplayanerrormessage.ThisisafeatureofPHPtomakeiteasiertoinsertasingleechostatement.Toseetheerrorwhenyoutrytorunthefirstechostatement,copythestatementtotwoseparatelinessoitlookslikethis:echo“Thiswon?twork.”echo“Thiswon?twork.”ThecodewillnotrunandPHPwillreturnanerrorbecausethereisnZasemicolonseparatingthestatements.5HowEmbeddedProgrammingWorksBeforenow,I?/eonlymentionedthatPHPcodemustbeenclosedinthe<?phpand?>PHPtags.UsingtagstoseparatePHPcodeandHTMLcodewithinthesamefileallowsprogrammingcodetobemixeddirectlywithinformationthatisgoingtobesenttothebrowserjustasitis.ThismakesPHPanembeddedprogramminglanguagebecausePHPcodeisembeddeddirectlyinHTMLcode.Thisconceptisrelativelynew:BeforelanguageslikePHP,programshadnorealneedtodisplaydatausingastructuredformattinglanguageascomplexasHTML.Informationdisplayedonthescreenwasusuallyjustletters,numbers,andspaces,withoutmanycolors,sizes,orotherformattingmarkups.SincePHPwasmadeforWebprogramming,itisintendedtobeusedwithHTML,whichsignificantlyincreasestheamountofinformationthathastobesentbacktothebrowser.NotonlydoesPHPhavetosendbacktheinformationtheusersees,butalsothemarkuptagsrequiredtoformattheinformationcorrectly.Tomakethemixingofinformationandmarkuptagssimpler,PHPcodeisembeddeddirectlyintheHTMLpagewheretheinformationisdesired.Theexampleatthebeginningofthischapterdemonstratesthisconceptquiteclearly;theprogramismostlyregularHTMLcode,butPHPisalsousedtoinsertsomeinformation.Embeddedprogrammingwillmakeyourjobasaprogrammermucheasier;youcanaddprogrammingwhereyouneeditanduseregularHTMLtherestofthetime.However,besuretoencloseyourPHPcodeinPHPtagsoryourcodewillnotbeparsed,butratherdisplayedontheHTMLpage.Thefoll

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論