首页 > 开发 > PHP > 正文

PHP 简介

2024-05-04 22:56:28
字体:
来源:转载
供稿:网友
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 年早些时候出版。可以通过 [email protected] 与他联系。



以下为英文原文

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

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开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表