长沙北大青鸟作者:科泰校区
Joe Brockmeier 简要介绍了 PHP 脚本语言,讨论了 PHP 的起源、性能和适用的平台。一个简单的 PHP 脚本示例则着重说明了其基本语法和用法。
如果您从事基于 Web 的开发工作,那么可能已经听说过 PHP。您也许不太确切地知道 PHP 是什么、如何工作或者为什么如此热门,但现在该是进一步了解 PHP 的时候了。因此本文简要介绍了关于 PHP 基础的基本概念。
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.
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.