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 适用的平台 许可证和使用 PHP 并没有得到 GPL 的许可,但它自己的许可证允许重新分发代码和/或二进制文件。 使用 PHP "Hello, World!" 如早些时候所提到的,PHP 是嵌入在 HTML 中的。(可能您的文件几乎没有包含 HTML,但是通常这个文件是 PHP 与 HTML 的混合体。)这意味着在您正常的 HTML 中(或 XHTML,如果您处在比较前沿的位置),会有类似这样的 PHP 语句: How to say "Hello, World!"
在 Web 页面上打印日期和时间 An Example of PHP in Action
此代码生成以下输出:
请注意,这里揉和了 PHP 和 HTML。假设您已经了解 HTML,所以这里仅解释 PHP 代码。在 PHP.net (请参阅参考资料)上可以找到完整的 PHP 参考。 PHP 代码是以标记 开始并以 ?> 结束的。这就告诉服务器在 和 ?> 之间的所有内容需要用 PHP 指令进行语法分析,如果发现它们,就需要执行它们。请注意,当处理和服务您的文件时,客户机会收到普通的 HTML 文件。浏览您站点的人看不到任何一个的 PHP 指令,除非您犯了错误,服务器把这些 PHP 代码分割开而没有先处理它们。 一般情况下会处理在 和 ?> 之间的常规 HTML 标记。请注意上面这个简单的脚本中包含了这个分行标记。如果不能够很好地利用 HTML 格式,那么 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. A bit of background 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 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 Licensing and use PHP is not licensed under the GPL, but its own license permits redistribution of code and/or binaries. Using PHP "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: How to say "Hello, World!"
Printing date and time in a Web page An Example of PHP in Action
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 and ends with ?>. This tells the server that everything between 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 and ?> will be processed normally. Note that the simple script above contains a 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 within the 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 Resources PHP.net is the main PHP Web site. |