Joe Brockmeier 简要介绍了 PHP 脚本语言,讨论了 PHP 的起源、性能和适用的平台。一个简单的 PHP 脚本示例则着重说明了其基本语法和用法。 如果您从事基于 Web 的开发工作,那么可能已经听说过 PHP。您也许不太确切地知道 PHP 是什么、如何工作或者为什么如此热门,但现在该是进一步了解 PHP 的时候了。因此本文简要介绍了关于 PHP 基础的基本概念。 PHP 是一种嵌入在 HTML 并由服务器解释的脚本语言。它可以用于管理动态内容、支持数据库、处理会话跟踪,甚至构建整个电子商务站点。它支持许多流行的数据库,包括 MySQL、PostgreSQL、Oracle、Sybase、Informix 和 Microsoft SQL Server。 另一方面,假设您从创建 product.php 页面开始。它没有静态信息,而是编码成可以从产品数据库中提取信息并动态地构建一个页面。然后您就拥有了一个元数据页面,它可以根据存储在数据库中的信息提供一个、一百个、甚至十万个单独页面。现在网站管理员不再整天都简单重复更新静态页面的工作,因为在更新公司数据库中的信息同时就可以更新页面上的信息。这样就消除了令人头疼的时间延迟(在数据库中更改信息和在网站上显示信息之间的时间间隔)。 总体来说,PHP 非常适合 Web 上的工作。但它并不是唯一的方法;如 Perl、Java、JavaScript、ASP、Python、Tcl、CGI 以及其它许多方法都可以生成动态的内容。但是,PHP 的优点是:它是专为基于 Web 的问题而设计的以及它是开放源码。 如果您正在为文字处理或 3D 游戏寻找程序设计语言,那 PHP 可能不是您所需要的语言。如果您需要运行一个具有动态内容、数据库交互和电子贸易的网站,那么就请继续读下去,因为 PHP 在这方面确实是非常有用的。 PHP 并没有得到 GPL 的许可,但它自己的许可证允许重新分发代码和/或二进制文件。 使用 PHP "Hello, World!" 如早些时候所提到的,PHP 是嵌入在 HTML 中的。(可能您的文件几乎没有包含 HTML,但是通常这个文件是 PHP 与 HTML 的混合体。)这意味着在您正常的 HTML 中(或 XHTML,如果您处在比较前沿的位置),会有类似这样的 PHP 语句:
很简单,不是吗?这仅仅是一个 "echo" 语句,就这样。当然,仅仅这样是没有多大用处的。但是它确实告诉我们关于语言的一些东西。(顺便说一下,如果检查 HTML 输出,就会注意到 PHP 的代码并没有出现在从服务器送到您 Web 浏览器的文件中。所有出现在 Web 页面中的 PHP 都会被处理并从页面中剥离;从 WEB 服务器返回给客户机的仅仅是纯 HTML 输出。) 在 Web 页面上打印日期和时间
此代码生成以下输出: The Current Date and Time is: 11:00 AM Friday, October 20 2000. 请注意,这里揉和了 PHP 和 HTML。假设您已经了解 HTML,所以这里仅解释 PHP 代码。在 PHP.net (请参阅参考资料)上可以找到完整的 PHP 参考。 PHP 代码是以标记 一般情况下会处理在 如果想和其它东西一起使用,或者您和我一样是一个健忘的人,您可能会想到要注释代码。// 字符表明是注释,服务器不会处理被 // 标记的内容,也不会象 HTML 中的注释一样,把内容传给客户机。如果在 最后要注意的是,每个 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 仍然是很好的示例和学*资料。 参考资料
关于作者
以下为英文原文 ======================================================= Finally, the perfect language for dynamic content and database interaction Joe "Zonker" Brockmeier
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. 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!" 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:
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
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 Regular HTML tags within the 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 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 |