Joe Brockmeier 簡要介紹了 PHP 腳本語言,討論了 PHP 的起源、性能和適用的平臺。一個簡單的 PHP 腳本示例則著重說明了其基本語法和用法。
如果您從事基于 Web 的開發工作,那么可能已經聽說過 PHP。您也許不太確切地知道 PHP 是什么、如何工作或者為什么如此熱門,但現在該是進一步了解 PHP 的時候了。因此本文簡要介紹了關于 PHP 基礎的基本概念。
一點背景知識
PHP 是作為一個小開放源碼,隨著越來越多的人意識到它的實用性從而逐漸發展起來。Rasmus Lerdorf 在 1994 年發布了 PHP 的第一個版本。從那時起它就飛速發展,并在原始發行版上經過無數的改進和完善現在已經發展到版本 4.0.3 。
PHP 是一種嵌入在 HTML 并由服務器解釋的腳本語言。它可以用于管理動態內容、支持數據庫、處理會話跟蹤,甚至構建整個電子商務站點。它支持許多流行的數據庫,包括 MySQL、PostgreSQL、Oracle、Sybase、Informix 和 Microsoft SQL Server。
另一方面,假設您從創建 product.php 頁面開始。它沒有靜態信息,而是編碼成可以從產品數據庫中提取信息并動態地構建一個頁面。然后您就擁有了一個元數據頁面,它可以根據存儲在數據庫中的信息提供一個、一百個、甚至十萬個單獨頁面?,F在網站管理員不再整天都簡單重復更新靜態頁面的工作,因為在更新公司數據庫中的信息同時就可以更新頁面上的信息。這樣就消除了令人頭疼的時間延遲(在數據庫中更改信息和在網站上顯示信息之間的時間間隔)。
總體來說,PHP 非常適合 Web 上的工作。但它并不是唯一的方法;如 Perl、Java、JavaScript、ASP、Python、Tcl、CGI 以及其它許多方法都可以生成動態的內容。但是,PHP 的優點是:它是專為基于 Web 的問題而設計的以及它是開放源碼。
如果您正在為文字處理或 3D 游戲尋找程序設計語言,那 PHP 可能不是您所需要的語言。如果您需要運行一個具有動態內容、數據庫交互和電子貿易的網站,那么就請繼續讀下去,因為 PHP 在這方面確實是非常有用的。
PHP 適用的平臺
大多數常規 PHP 的安裝通常是與 Linux 或各種 UNIX 上的 Apache 一起運行的 PHP 模塊。但是如果正在使用其它平臺,不要擔心。PHP 可以在 Windows NT 和 9x 以及其它許多 Web 服務器上運行??梢栽谥饕榻B Apache/Linux/PHP 組合的一些網站上找到更多有關 PHP 的文檔,但它并不是支持 PHP 的唯一平臺。
許可證和使用
購買具有全部功能的嵌入式 Web 腳本語言要花多少錢?一分錢不花?PHP 是一個開放源碼項目,所以沒有購買許可證的費用或限制使用的問題。您可以使用 PHP 來運行小的、非贏利性站點,或者運行十億美元的電子商務網站,而且成本是一樣的:零。不僅如此,如果想要或需要修改 PHP,可以修改它。
PHP 并沒有得到 GPL 的許可,但它自己的許可證允許重新分發代碼和/或二進制文件。
使用 PHP
好,現在您已經確信要真正地嘗試一下 PHP 了吧。我們先看一些簡單的例子,這樣您對 PHP 就有個大概了解。記住決這不是深入了解 PHP 的途徑,僅僅是個快速入門而已。
"Hello, World!"
為了對 PHP 有個了解,讓我們來看一下幾個非常簡單的 PHP 腳本。既然 "Hello, World!" 是個常用的示例,那我們就編寫一個友好的小 "Hello, World!" 腳本。
如早些時候所提到的,PHP 是嵌入在 HTML 中的。(可能您的文件幾乎沒有包含 HTML,但是通常這個文件是 PHP 與 HTML 的混合體。)這意味著在您正常的 HTML 中(或 XHTML,如果您處在比較前沿的位置),會有類似這樣的 PHP 語句:
<body bgcolor="white"> <strong>How to say "Hello, World!"</strong> <?php echo "Hello, World!";?> <br> Simple, huh? </body> |
很簡單,不是嗎?這僅僅是一個 "echo" 語句,就這樣。當然,僅僅這樣是沒有多大用處的。但是它確實告訴我們關于語言的一些東西。(順便說一下,如果檢查 HTML 輸出,就會注意到 PHP 的代碼并沒有出現在從服務器送到您 Web 瀏覽器的文件中。所有出現在 Web 頁面中的 PHP 都會被處理并從頁面中剝離;從 WEB 服務器返回給客戶機的僅僅是純 HTML 輸出。)
在 Web 頁面上打印日期和時間
現在我們做一些稍微實用的事情。這個示例將在 Web 頁面上打印日期和時間。
<body bgcolor="white"> <strong>An Example of PHP in Action</strong> <?php echo "The Current Date and Time is:<br>"; echo date("g:i A l, F j Y.");?> // g = the hour, in 12-hour format // i = minutes // A = print AM or PM, depending... // l = print the day of the week // F = print the month // j = print the day of the month // Y = print the year - all four digits |
此代碼生成以下輸出:
The Current Date and Time is: 11:00 AM Friday, October 20 2000.
請注意,這里揉和了 PHP 和 HTML。假設您已經了解 HTML,所以這里僅解釋 PHP 代碼。在 PHP.net (請參閱參考資料)上可以找到完整的 PHP 參考。
PHP 代碼是以標記 <?php
開始并以 ?>
結束的。這就告訴服務器在 <?php
和 ?>
之間的所有內容需要用 PHP 指令進行語法分析,如果發現它們,就需要執行它們。請注意,當處理和服務您的文件時,客戶機會收到普通的 HTML 文件。瀏覽您站點的人看不到任何一個的 PHP 指令,除非您犯了錯誤,服務器把這些 PHP 代碼分割開而沒有先處理它們。
一般情況下會處理在 <?php
和 ?>
之間的常規 HTML 標記。請注意上面這個簡單的腳本中包含了 <br>
這個分行標記。如果不能夠很好地利用 HTML 格式,那么 PHP 就不會非常有用。
如果想和其它東西一起使用,或者您和我一樣是一個健忘的人,您可能會想到要注釋代碼。// 字符表明是注釋,服務器不會處理被 // 標記的內容,也不會象 HTML 中的注釋一樣,把內容傳給客戶機。如果在 <?php
和 ?>
標記之間有標準的 <!-- comment -->
,那么當服務器對它進行語法分析時,有可能引起錯誤。顯然,您可能不會象我對這個基本功能一樣注釋您的代碼,但它確是一個很好的示例。
最后要注意的是,每個 PHP 函數都封閉在圓括號內,并以分號結束,這和 C 或 Perl 相似。由于一個簡單的印刷錯誤而遺漏一個結束的圓括號或分號,造成一些語法錯誤是很常見的,所以要確保檢查代碼。在象 Vim 或 Emacs 這樣的編輯器(可以突出顯示語法)中編寫 PHP 是有助于消除此類錯誤。它使您能立刻捕捉到錯誤。
date 函數僅是內置 PHP 函數之一。PHP 附帶了許多功能可以用于數據庫連接、創建 PDF、Shockwave、JPG、GIF、PNG 和其它圖象文件、發送電子郵件、閱讀和書寫文件、語法分析 XML、會話處理、經由 HTTP 直接與瀏覽器對話,以及許多其它功能。
PHP 也允許用戶定義自己的函數。這使 PHP 語言能夠經由 Web 提供大量的解決方案。而不是從一開始就把所有的事情都編寫好了。在您編寫函數之前,確保已經查看諸如 Zend.com、PHP Wizard 當然還有 Freshmeat,來看是否已經有您正在嘗試編寫的函數(請參閱參考資料)。
對于提供標題、自動更新新聞網站、基于 Web 的電子郵件客戶機、數據庫管理和其它方面,已經有許多開放源碼的 PHP 解決方案。再從頭來做這些是毫無意義的。相反,應該從已經構建好的基礎開始,并把它定制到您自己的解決方案中。如果您僅僅是初步了解和學習 PHP,在心中并沒有具體的項目,那么這些項目對于使用 PHP 仍然是很好的示例和學習資料。
參考資料
關于作者
Joe "Zonker" Brockmeier 是 Linux Magazine 的攥稿編輯,他為 Prima 出版社寫過 Install, Configure and Customize Slackware Linux 一書。他的第二本書,DocBook Publishing,將由 Prima 于 2001 年早些時候出版??梢酝ㄟ^ jbrockmeier@earthlink.net 與他聯系。
以下為英文原文
=======================================================
Finally, the perfect language for dynamic content and database interaction
Joe "Zonker" Brockmeier
Senior Editor, User Friendly Media
December 2000
clearcase/" target="_blank" >cc6633>內容: | ||||||||
![]() | ||||||||
![]() | ||||||||
| ||||||||
![]() |
Joe Brockmeier presents a brief introduction to the PHP scripting language with a discussion of PHP's origins, capabilities, and the platforms it's available on. A simple PHP script example highlights basic syntax and usage.
If you work with Web-based development, you've probably heard about PHP. You might not know exactly what it is, how it works, or why it's so hot, but you do know it's time to find out more about it. So here's a quick intro to the basic concepts that underlie PHP.
A bit of background
PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994. It has been picking up steam ever since and is now at version 4.0.3 with numerous improvements and refinements over the original release.
PHP is a scripting language that is embedded in HTML and interpreted by the server. It can be used to manage dynamic content, work with databases, handle session tracking, and even build entire e-commerce sites. It works well with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
On the other hand, let's say you start by creating one page that is called product.php. Instead of holding static information, it's coded to pull information out of your product database and build a page dynamically. You then have one meta page that can serve up one or one hundred or even a hundred thousand unique pages based on information stored in a database. Rather than requiring a Web master to spend an entire day doing nothing but monkey-work updating static Web pages, the information can now be updated at the same time the information is changed in the company database. You eliminate the headache-inducing lag between the time information is changed in the database and the time it makes its way onto the Web site.
At the risk of generalizing, for work on the Web, PHP is a great way to go. It's not the only way to go; Perl, Java, JavaScript, ASP, Python, Tcl, CGIs, and probably dozens of other ways are available for generating dynamic content. However, PHP has the benefit of being designed just for Web-based problems and of being an open source project.
If you're looking for a programming language for a word processor or 3D game, then PHP probably isn't the way to go. If you need to run a Web site with dynamic content, database interaction, and e-commerce, read on, because PHP is going to be very helpful indeed.
PHP is not licensed under the GPL, but its own license permits redistribution of code and/or binaries.
"Hello, World!"
To get a feel for PHP, let's look at some very simple PHP scripts. Since "Hello, World!" is an obligatory example, we'll produce a friendly little "Hello, World!" script.
As mentioned earlier, PHP is embedded in HTML. (You could have a file that contains almost no HTML, but usually it's a mixture.) That means that in amongst your normal HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this:
<body bgcolor="white"> <strong>How to say "Hello, World!"</strong> <?php echo "Hello, World!";?> <br> Simple, huh? </body> |
Simple, right? Just an "echo" statement, and that's it. Of course, that on its own isn't terribly useful. But it does teach us something about the language. (By the way, if you examine the HTML output, you'll notice that the PHP code is not present in the file sent from the server to your Web browser. All of the PHP present in the Web page is processed and stripped from the page; the only thing returned to the client from the Web server is pure HTML output.)
Printing date and time in a Web page
Now we'll do something a little more useful. This example will print out the date and time in a Web page.
<body bgcolor="white"> <strong>An Example of PHP in Action</strong> <?php echo "The Current Date and Time is:<br>"; echo date("g:i A l, F j Y.");?> // g = the hour, in 12-hour format // i = minutes // A = print AM or PM, depending... // l = print the day of the week // F = print the month // j = print the day of the month // Y = print the year - all four digits |
This code produces the output:
The Current Date and Time is: 11:00 AM Friday, October 20 2000.
Notice the blend of PHP and HTML here. I'll assume that you can already read HTML, so I'll only explain the PHP code. You can find a complete PHP reference online at PHP.net (see Resources).
The PHP code begins with the tag <?php
and ends with ?>
. This tells the server that everything between <?php
and ?>
needs to be parsed for PHP instructions, and that if they're found, they need to be executed. Note that when your document is processed and served, it will be received by the client as plain HTML. Someone browsing your site will not see any of your PHP instructions, unless you've made an error and the server spits them out as-is instead of processing them first.
Regular HTML tags within the <?php
and ?>
will be processed normally. Note that the simple script above contains a <br>
line-break tag. PHP wouldn't be very useful if you couldn't include HTML formatting as well.
If you're going to be working with others, or if you're just plain forgetful like me, you'll want to comment your code as well. The // characters indicate a comment, which the server will not process or pass on to the client, unlike comments in HTML. If you include a standard <!-- comment -->
within the <?php
and ?>
tags, it is likely to cause an error when parsed by the server. Obviously, you probably wouldn't comment your code quite so much as I have above for such a basic function, but it makes a good example.
Finally, note that each PHP function is enclosed in parentheses and ends with a semicolon, which will seem familiar to fans of C or Perl. It's not uncommon to forget a closing parenthesis or semicolon and have a number of parse errors just because of a simple typo, so be sure to check them. It's helpful to edit PHP in an editor like Vim or Emacs that is capable of syntax highlighting. This allows you to catch your errors right away.
The date function is just one of the built-in PHP functions. PHP also comes with functions for database connectivity, creating PDF, Shockwave, JPG, GIF, PNG, and other graphics files, sending e-mail, reading and writing files, parsing XML, session handling, talking directly to the browser via HTTP, and many other functions.
PHP also allows the user to define their own functions. This makes PHP a language capable of providing a huge number of solutions via the Web. Rather than just writing everything from scratch, however, be sure to check sites like Zend.com, PHP Wizards, and, of course, Freshmeat to see if what you're trying to do has been done already (see Resources).
There are a lot of open sourced PHP solutions for serving banners, automating news sites, Web-based e-mail clients, database management, and much more. There's no sense re-inventing the wheel. Instead, start from the foundation that has already been built and customize it into your own solution. If you're just poking around with PHP to learn it and don't have a specific project in mind, these projects are still great examples of what you can do with PHP and serve as great learning resources.
Resources
About the author
Joe "Zonker" Brockmeier is a contributing editor for Linux Magazine and has written Install, Configure and Customize Slackware Linux for Prima Publishing. His second book, DocBook Publishing, will be published by Prima in early 2001. He can be reached at jbrockmeier@earthlink.net.
原文轉自:http://www.anti-gravitydesign.com