当前位置:首页> PHP教程> 经典文章
关键字
文章内容
PHP 简介
 
 
修改时间:[2010/07/03 11:39]    阅读次数:[684]    发表者:[起缘]
 

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。

动态与静态内容
动态内容为什么这么热门?假设您正在管理有 10 个产品的电子商务站点。只要产品不是经常变动或者预料到它不会有太大的变动,那么手工编写 10 个带有必要的信息、表单和诸如此类内容的静态产品页面是不困难的。但是,假设您在本月再要添加 10 个或更多产品,然后在下个月要更多,而且价格有时会变动或者想改变站点的观感。那么您就会陷入用手工重新编写数十个,也许上百个静态页面的困境中。

另一方面,假设您从创建 product.php 页面开始。它没有静态信息,而是编码成可以从产品数据库中提取信息并动态地构建一个页面。然后您就拥有了一个元数据页面,它可以根据存储在数据库中的信息提供一个、一百个、甚至十万个单独页面。现在网站管理员不再整天都简单重复更新静态页面的工作,因为在更新公司数据库中的信息同时就可以更新页面上的信息。这样就消除了令人头疼的时间延迟(在数据库中更改信息和在网站上显示信息之间的时间间隔)。

总体来说,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 服务器上运行。可以在主要介绍 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 仍然是很好的示例和学习资料。

结束语
这篇 PHP 脚本语言的简要概述阐明了 PHP 功能和用法。在以后的文章中,我会讲述通过访问 MySQL 数据库来创建动态的 Web 产品页面。在那之前,请查看下面关于 PHP 的参考资料以了解更多信息。

参考资料

  • PHP.net 是主要的 PHP 网站。
  • PHP Wizards 包含一些文档和几个流行的 PHP 项目。
  • 作者 Julie C. Meloni 创建的 Thickbook.com 有许多有用的教程。Julie 是 PHP Essentials(最优秀的 PHP 书籍之一)的作者。
  • PHP Builder 是另一个流行的网站,它上面有一些教程和代码库。
  • Zend.com 包含大量有用的关于 PHP 的链接和信息。Zend 是 PHP 的优化器。
  • 请查看 Freshmeat 来得到关于 PHP 开发的一些更新,以及其它一些东西。
  • 请到 Perl.com 了解有关另一个流行的脚本语言的详细信息。
  • 请查看 MySQL 官方主页。
  • 请到 PHPMyAdmin 网站查看已解决的 PHP 和 MySQL 问题。
  • 官方的 Python 网站总是有用的。
  • Java Lobby 有许多有用的 Java 信息。

关于作者
Joe "Zonker" Brockmeier 是 Linux Magazine 的攥稿编辑,他为 Prima 出版社写过 Install, Configure and Customize Slackware Linux 一书。他的第二本书,DocBook Publishing,将由 Prima 于 2001 年早些时候出版。可以通过 与他联系。

 

以下为英文原文

=======================================================

Finally, the perfect language for dynamic content and database interaction

Joe "Zonker" Brockmeier
Senior Editor, User Friendly Media
December 2000

内容:
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 .

 
 
 
 
 
 

威尼斯人唯一官网