如早些时候所提到的,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.
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 [email protected] 中国最大的web开发资源网站及技术社区,