PHP 簡介
發(fā)表時(shí)間:2023-08-23 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]Joe Brockmeier 簡要介紹了 PHP 腳本語言,討論了 PHP 的起源、性能和適用的平臺。一個(gè)簡單的 PHP 腳本示例則著重說明了其基本語法和用法。如果您從事基于 Web 的開發(fā)工作,那...
Joe Brockmeier 簡要介紹了 PHP 腳本語言,討論了 PHP 的起源、性能和適用的平臺。一個(gè)簡單的 PHP 腳本示例則著重說明了其基本語法和用法。
如果您從事基于 Web 的開發(fā)工作,那么可能已經(jīng)聽說過 PHP。您也許不太確切地知道 PHP 是什么、如何工作或者為什么如此熱門,但現(xiàn)在該是進(jìn)一步了解 PHP 的時(shí)候了。因此本文簡要介紹了關(guān)于 PHP 基礎(chǔ)的基本概念。
一點(diǎn)背景知識
PHP 是作為一個(gè)小開放源碼,隨著越來越多的人意識到它的實(shí)用性從而逐漸發(fā)展起來。Rasmus Lerdorf 在 1994 年發(fā)布了 PHP 的第一個(gè)版本。從那時(shí)起它就飛速發(fā)展,并在原始發(fā)行版上經(jīng)過無數(shù)的改進(jìn)和完善現(xiàn)在已經(jīng)發(fā)展到版本 4.0.3 。
PHP 是一種嵌入在 HTML 并由服務(wù)器解釋的腳本語言。它可以用于管理動態(tài)內(nèi)容、支持?jǐn)?shù)據(jù)庫、處理會話跟蹤,甚至構(gòu)建整個(gè)電子商務(wù)站點(diǎn)。它支持許多流行的數(shù)據(jù)庫,包括 MySQL、PostgreSQL、Oracle、Sybase、Informix 和 Microsoft SQL Server。
動態(tài)與靜態(tài)內(nèi)容
動態(tài)內(nèi)容為什么這么熱門?假設(shè)您正在管理有 10 個(gè)產(chǎn)品的電子商務(wù)站點(diǎn)。只要產(chǎn)品不是經(jīng)常變動或者預(yù)料到它不會有太大的變動,那么手工編寫 10 個(gè)帶有必要的信息、表單和諸如此類內(nèi)容的靜態(tài)產(chǎn)品頁面是不困難的。但是,假設(shè)您在本月再要添加 10 個(gè)或更多產(chǎn)品,然后在下個(gè)月要更多,而且價(jià)格有時(shí)會變動或者想改變站點(diǎn)的觀感。那么您就會陷入用手工重新編寫數(shù)十個(gè),也許上百個(gè)靜態(tài)頁面的困境中。
另一方面,假設(shè)您從創(chuàng)建 product.php 頁面開始。它沒有靜態(tài)信息,而是編碼成可以從產(chǎn)品數(shù)據(jù)庫中提取信息并動態(tài)地構(gòu)建一個(gè)頁面。然后您就擁有了一個(gè)元數(shù)據(jù)頁面,它可以根據(jù)存儲在數(shù)據(jù)庫中的信息提供一個(gè)、一百個(gè)、甚至十萬個(gè)單獨(dú)頁面,F(xiàn)在網(wǎng)站管理員不再整天都簡單重復(fù)更新靜態(tài)頁面的工作,因?yàn)樵诟鹿緮?shù)據(jù)庫中的信息同時(shí)就可以更新頁面上的信息。這樣就消除了令人頭疼的時(shí)間延遲(在數(shù)據(jù)庫中更改信息和在網(wǎng)站上顯示信息之間的時(shí)間間隔)。
總體來說,PHP 非常適合 Web 上的工作。但它并不是唯一的方法;如 Perl、Java、JavaScript、ASP、Python、Tcl、CGI 以及其它許多方法都可以生成動態(tài)的內(nèi)容。但是,PHP 的優(yōu)點(diǎn)是:它是專為基于 Web 的問題而設(shè)計(jì)的以及它是開放源碼。
如果您正在為文字處理或 3D 游戲?qū)ふ页绦蛟O(shè)計(jì)語言,那 PHP 可能不是您所需要的語言。如果您需要運(yùn)行一個(gè)具有動態(tài)內(nèi)容、數(shù)據(jù)庫交互和電子貿(mào)易的網(wǎng)站,那么就請繼續(xù)讀下去,因?yàn)?PHP 在這方面確實(shí)是非常有用的。
PHP 適用的平臺
大多數(shù)常規(guī) PHP 的安裝通常是與 Linux 或各種 UNIX 上的 Apache 一起運(yùn)行的 PHP 模塊。但是如果正在使用其它平臺,不要擔(dān)心。PHP 可以在 Windows NT 和 9x 以及其它許多 Web 服務(wù)器上運(yùn)行。可以在主要介紹 Apache/Linux/PHP 組合的一些網(wǎng)站上找到更多有關(guān) PHP 的文檔,但它并不是支持 PHP 的唯一平臺。
許可證和使用
購買具有全部功能的嵌入式 Web 腳本語言要花多少錢?一分錢不花?PHP 是一個(gè)開放源碼項(xiàng)目,所以沒有購買許可證的費(fèi)用或限制使用的問題。您可以使用 PHP 來運(yùn)行小的、非贏利性站點(diǎn),或者運(yùn)行十億美元的電子商務(wù)網(wǎng)站,而且成本是一樣的:零。不僅如此,如果想要或需要修改 PHP,可以修改它。
PHP 并沒有得到 GPL 的許可,但它自己的許可證允許重新分發(fā)代碼和/或二進(jìn)制文件。
使用 PHP
好,現(xiàn)在您已經(jīng)確信要真正地嘗試一下 PHP 了吧。我們先看一些簡單的例子,這樣您對 PHP 就有個(gè)大概了解。記住決這不是深入了解 PHP 的途徑,僅僅是個(gè)快速入門而已。
"Hello, World!"
為了對 PHP 有個(gè)了解,讓我們來看一下幾個(gè)非常簡單的 PHP 腳本。既然 "Hello, World!" 是個(gè)常用的示例,那我們就編寫一個(gè)友好的小 "Hello, World!" 腳本。
如早些時(shí)候所提到的,PHP 是嵌入在 HTML 中的。(可能您的文件幾乎沒有包含 HTML,但是通常這個(gè)文件是 PHP 與 HTML 的混合體。)這意味著在您正常的 HTML 中(或 XHTML,如果您處在比較前沿的位置),會有類似這樣的 PHP 語句:
<body bgcolor="white">
<strong>How to say "Hello, World!"</strong>
<?php echo "Hello, World!";?>
<br>
Simple, huh?
</body>
很簡單,不是嗎?這僅僅是一個(gè) "echo" 語句,就這樣。當(dāng)然,僅僅這樣是沒有多大用處的。但是它確實(shí)告訴我們關(guān)于語言的一些東西。(順便說一下,如果檢查 HTML 輸出,就會注意到 PHP 的代碼并沒有出現(xiàn)在從服務(wù)器送到您 Web 瀏覽器的文件中。所有出現(xiàn)在 Web 頁面中的 PHP 都會被處理并從頁面中剝離;從 WEB 服務(wù)器返回給客戶機(jī)的僅僅是純 HTML 輸出。)
在 Web 頁面上打印日期和時(shí)間
現(xiàn)在我們做一些稍微實(shí)用的事情。這個(gè)示例將在 Web 頁面上打印日期和時(shí)間。
<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。假設(shè)您已經(jīng)了解 HTML,所以這里僅解釋 PHP 代碼。在 PHP.net (請參閱參考資料)上可以找到完整的 PHP 參考。
PHP 代碼是以標(biāo)記 <?php 開始并以 ?> 結(jié)束的。這就告訴服務(wù)器在 <?php 和 ?> 之間的所有內(nèi)容需要用 PHP 指令進(jìn)行語法分析,如果發(fā)現(xiàn)它們,就需要執(zhí)行它們。請注意,當(dāng)處理和服務(wù)您的文件時(shí),客戶機(jī)會收到普通的 HTML 文件。瀏覽您站點(diǎn)的人看不到任何一個(gè)的 PHP 指令,除非您犯了錯(cuò)誤,服務(wù)器把這些 PHP 代碼分割開而沒有先處理它們。
一般情況下會處理在 <?php 和 ?> 之間的常規(guī) HTML 標(biāo)記。請注意上面這個(gè)簡單的腳本中包含了 <br> 這個(gè)分行標(biāo)記。如果不能夠很好地利用 HTML 格式,那么 PHP 就不會非常有用。
如果想和其它東西一起使用,或者您和我一樣是一個(gè)健忘的人,您可能會想到要注釋代碼。// 字符表明是注釋,服務(wù)器不會處理被 // 標(biāo)記的內(nèi)容,也不會象 HTML 中的注釋一樣,把內(nèi)容傳給客戶機(jī)。如果在 <?php 和 ?> 標(biāo)記之間有標(biāo)準(zhǔn)的 <!-- comment --> ,那么當(dāng)服務(wù)器對它進(jìn)行語法分析時(shí),有可能引起錯(cuò)誤。顯然,您可能不會象我對這個(gè)基本功能一樣注釋您的代碼,但它確是一個(gè)很好的示例。
最后要注意的是,每個(gè) PHP 函數(shù)都封閉在圓括號內(nèi),并以分號結(jié)束,這和 C 或 Perl 相似。由于一個(gè)簡單的印刷錯(cuò)誤而遺漏一個(gè)結(jié)束的圓括號或分號,造成一些語法錯(cuò)誤是很常見的,所以要確保檢查代碼。在象 Vim 或 Emacs 這樣的編輯器(可以突出顯示語法)中編寫 PHP 是有助于消除此類錯(cuò)誤。它使您能立刻捕捉到錯(cuò)誤。
date 函數(shù)僅是內(nèi)置 PHP 函數(shù)之一。PHP 附帶了許多功能可以用于數(shù)據(jù)庫連接、創(chuàng)建 PDF、Shockwave、JPG、GIF、PNG 和其它圖象文件、發(fā)送電子郵件、閱讀和書寫文件、語法分析 XML、會話處理、經(jīng)由 HTTP 直接與瀏覽器對話,以及許多其它功能。
PHP 也允許用戶定義自己的函數(shù)。這使 PHP 語言能夠經(jīng)由 Web 提供大量的解決方案。而不是從一開始就把所有的事情都編寫好了。在您編寫函數(shù)之前,確保已經(jīng)查看諸如 Zend.com、PHP Wizard 當(dāng)然還有 Freshmeat,來看是否已經(jīng)有您正在嘗試編寫的函數(shù)(請參閱參考資料)。
對于提供標(biāo)題、自動更新新聞網(wǎng)站、基于 Web 的電子郵件客戶機(jī)、數(shù)據(jù)庫管理和其它方面,已經(jīng)有許多開放源碼的 PHP 解決方案。再從頭來做這些是毫無意義的。相反,應(yīng)該從已經(jīng)構(gòu)建好的基礎(chǔ)開始,并把它定制到您自己的解決方案中。如果您僅僅是初步了解和學(xué)習(xí) PHP,在心中并沒有具體的項(xiàng)目,那么這些項(xiàng)目對于使用 PHP 仍然是很好的示例和學(xué)習(xí)資料。
結(jié)束語
這篇 PHP 腳本語言的簡要概述闡明了 PHP 功能和用法。在以后的文章中,我會講述通過訪問 MySQL 數(shù)據(jù)庫來創(chuàng)建動態(tài)的 Web 產(chǎn)品頁面。在那之前,請查看下面關(guān)于 PHP 的參考資料以了解更多信息。
參考資料
PHP.net 是主要的 PHP 網(wǎng)站。
PHP Wizards 包含一些文檔和幾個(gè)流行的 PHP 項(xiàng)目。
作者 Julie C. Meloni 創(chuàng)建的 Thickbook.com 有許多有用的教程。Julie 是 PHP Essentials(最優(yōu)秀的 PHP 書籍之一)的作者。
PHP Builder 是另一個(gè)流行的網(wǎng)站,它上面有一些教程和代碼庫。
Zend.com 包含大量有用的關(guān)于 PHP 的鏈接和信息。Zend 是 PHP 的優(yōu)化器。
請查看 Freshmeat 來得到關(guān)于 PHP 開發(fā)的一些更新,以及其它一些東西。
請到 Perl.com 了解有關(guān)另一個(gè)流行的腳本語言的詳細(xì)信息。
請查看 MySQL 官方主頁。
請到 PHPMyAdmin 網(wǎng)站查看已解決的 PHP 和 MySQL 問題。
官方的 Python 網(wǎng)站總是有用的。
Java Lobby 有許多有用的 Java 信息。
關(guān)于作者
Joe "Zonker" Brockmeier 是 Linux Magazine 的攥稿編輯,他為 Prima 出版社寫過 Install, Configure and Customize Slackware Linux 一書。他的第二本書,DocBook Publishing,將由 Prima 于 2001 年早些時(shí)候出版。可以通過 jbrockmeier@earthlink.net 與他聯(lián)系。
以下為英文原文
=======================================================
Finally, the perfect language for dynamic content and database interaction
Joe "Zonker" Brockmeier
Senior Editor, User Friendly Media
December 2000
內(nèi)容:
Background
Dynamic vs. static content
Platforms
Licensing and use
Using PHP
Summary
Resources
About the author
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.
Dynamic vs. static content
What's so hot about dynamic content? Let's say you're managing an e-commerce site with 10 products. It's not that difficult to hand-code ten static product pages with all the requisite information, forms and such, provided your products don't change often and you don't anticipate much growth. However, let's say you add ten more products this month, and then more next month, and occasionally prices change or you want to change the look and feel of your site. Then you're stuck re-coding dozens, maybe hundreds, of static pages by hand.
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.
Platforms available for PHP
The most common PHP installation is probably the PHP module running with Apache on Linux or a UNIX-variant. But if you're using something else, don't worry. PHP works on Windows NT and 9x, as well as with a number of other Web servers. You'll find more documentation floating around on the Web that's specific to the Apache/Linux/PHP combo, but it isn't by any means the only platform that PHP is supported on.
Licensing and use
How much might you pay for a full-featured embedded Web scripting language? How about nothing? PHP is an open source project, so there are no licensing fees or restrictions on use. You could run a small, non-profit site, or you could run a billion-dollar e-commerce site with PHP, and the cost is the same: zero. Not only that, but the PHP code is available in case you want or need to tweak it.
PHP is not licensed under the GPL, but its own license permits redistribution of code and/or binaries.
Using PHP
Okay, so now you're convinced that you want to actually try PHP out. We'll walk through a few simple examples so you can get your feet wet. Keep in mind that this is by no means an exhaustive look at PHP, just a quick starter.
"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.
Summary
This brief overview of the PHP scripting language should give you an idea of what PHP is capable of and how PHP is being used. In a future article, I'll walk through accessing a MySQL database to create a dynamic Web product page. Until then, check out the resources on PHP below for further information.
Resources
PHP.net is the main PHP Web site.
PHP Wizards contains documentation and several popular PHP projects.
Thickbook.com, by Julie C. Meloni, has a number of very helpful tutorials. Julie is the author of PHP Essentials, one of the best PHP books available.
PHP Builder is another popular PHP site with tutorials and code libraries.
Zend.com contains plenty of useful links and information about PHP. Zend is the PHP Optimizer.
Check out Freshmeat to get updates on PHP development, among other things.
Go to Perl.com to find out more about another popular scripting language.
Check out the MySQL official home page.
Go to the PHPMyAdmin site to see PHP and MySQL handled.
The official Python site is always useful.
The Java Lobby has lots of useful Java info.
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.