PHP毕业设计英文文献翻译

更新时间:2024-05-30 02:31:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

本科毕业设计(论文)

外文参考文献译文及原文

学 院 信息工程学院 专 业 信息工程(电子信息工程方向) 年级班别 2004级(4)班 学 号 3104002975 学生姓名 陈英权 指导教师 刘喜英

2008 年 6 月 5 日

目 录

外文参考文献译文

1 为什么选择PHP ......................................... 错误!未定义书签。 2 如果你是编程新手 ...................................... 错误!未定义书签。 3 写一个基本的PHP程序 .................................. 错误!未定义书签。 4 编程语法 .............................................. 错误!未定义书签。 5 嵌入式语言如何工作 .................................... 错误!未定义书签。 6 服务端和客户端脚本 .................................... 错误!未定义书签。 7 运行你的程序 .......................................... 错误!未定义书签。

外文参考文献原文

1 Why PHP? ............................................... 错误!未定义书签。 2 If You Are New to Programming .......................... 错误!未定义书签。 3 Writing a Basic PHP Program ............................ 错误!未定义书签。 4 Programming Syntax ..................................... 错误!未定义书签。 5 How Embedded Programming Works ......................... 错误!未定义书签。 6 Server-side Versus Client-side Scripting ............... 错误!未定义书签。 7 Running Your New Program .............................. 错误!未定义书签。

1

1 为什么选择PHP

对于Web编程来说,PHP是一个很好的选择。它较其它语言(包括其它面向Web的语言)有许多优点。为了得到一个清晰的理解(和常见的面向Web的语言相比),让我们将它们比较一下。

ASP是微软的网络开发环境(它本身不是一门开发语言,因为它允许程序员在ASP中选择其它语言进行开发,如VBScript或JScript。)ASP虽然简单,但它太过于简单了,以至于不能使用更复杂的逻辑和算法。

除了ASP的过分简单,很多公司发现很难在微软的ASP许可证上节约成本。即使不考虑硬件成本,微软的Web服务器就要数千美元,而基于UNIX操作系统的、可运行PHP的Web服务器则是免费的。

另一种Web使用的知名语言是Sun Microsystems公司的Java。Java是平台独立的语言(在一个系统上用Java开发的程序可以不经过任何修改,就可以运行在其它系统上)。

尽管Java的确有它的优点,但它在开发时间、开发成本和执行速度上却有明显的劣势。Java开发是耗时的,因为Java工程要遵循严格的规则(Java强制的),这需要进行大量的规划。另外,Java开发周期长,费用成本高, Java程序员的待遇也较高。因此Java程序开发的成本远远高于其它语言开发的成本。通过我们的比较可以看出,即使项目开发结束后,以Java语言写的程序在运行时所花费的时间比其它语言的时间还要长。

总的来说,和Java相比,PHP的表现很出色。一个相似的工程以PHP语言开发,要比用Java开发节省两三倍的时间。最重要的是,虽然最终的程序都可以运行多种平台上(如Java),而PHP的程序则运行的更快。

另一种开发Web程序的常用语言是Perl。Perl与PHP一样,是一个可运行在多个平台上的开源项目。事实上,Perl的历史远远要比PHP长。在PHP之前,Perl被认为是最好的Web开发语言。然而,在过去的几年中,在Web开发领域,PHP赢得了比Perl更好的口碑。因为PHP语言本身集成了大量的功能模块,而要在Perl中使用这些功能,你就不得不单独下载许多额外的功能模块。这样,当程序从一个系统转移到另一个系统时,就会容易产生问题,因为Perl不得不从自己的CPAN上下载相应的功能模块。

最后要比较的语言是C。C语言已经存在很长的时间了;它已被用在各种各样的计算机中,从大型主机到个人PC都可见到它的身影。如果你了解C语言的话,你就会知

2

道用C语言开发Web程序会存在一些问题。为了用C语言开发一个Web程序,在你着手构思你的实际任务时,你将不得不着手实现所有Web程序本身就应该具备的功能(如采集HTML表单提交的数据)。而PHP语言提供了所有的Web常用或者并不是很常用的功能,可以允许程序员将更多的精力放在实际的任务中。

就其它的Web开发语言与PHP语言之间的比较而言,它们之间还存在很多不同。在Internet上将PHP与Java,Perl,ASP等语言进行比较的文章也很多。但是一旦你拥有了PHP的编程经验,你可能就会发现,在选择开发语言时,你会努力的说服你的客户或老板,允许你使用PHP来进行开发,而不是选择其它的语言。如果出现问题,你可以通过搜索Internet找到大量基于这些问题的有用信息。

PHP的使用是没有限制的。早期的版本使用的是单独的跟踪程序。而随着时间的推移,这个简单的跟踪程序已经被包含在语言的内部了。

PHP的主要用途,当然不只是用来跟踪的了;它还可以做许多其它的事情。为了让你理解PHP可以做什么,下面介绍了一些PHP的常见用途:

? 反馈表单

? 购物车和其它类型的电子商务系统 ? 用户注册,访问控制,以及在线订阅服务 ? 客户登记 ? 讨论和留言板

2 如果你是编程新手

如果你是一个从来没有写过程序的新手,这个想法可能有点吓人。大多数程序员可能会告诉你(如果他们愿意承认),他们刚开始学习编程的时候,也有点无从下手。然而,编程的过程根本不是那么难,相反却很享受,因为你不需要有多高的智商来适应它。

当你写程序的时候,你的主要目的就是把你的想法翻译成计算机能够识别的语言。例如,如果你是教人如何做汉堡包,你会首先描述将牛肉做成牛肉饼的过程。然后,你会告诉他如何把汉堡包放在烤架上,烤多长时间,最后,如何把它们拿下来。

当然,仅仅因为你能描述制作汉堡包的过程,并不意味着PHP能够在任何时间、任何地点为你做任何事情。重点是,如果你能像我刚才描述制作汉堡包的过程那样,描

3

述你的想法及思路,你就可以写程序。

写程序的过程就是告诉PHP如何做事情的过程。当你读完本文的时候,你就会明白围绕在PHP程序背后的所有概念,这些概念就像描述制作汉堡包的过程一样。对于本文,你读的越透,你明白的概念就越多,你向PHP描述的任务就越准确。因此,如果你很快学会用PHP程序来实现你的需求和想法,你就不会再感觉到烦恼了。

整体说来,有些编程问题可能是非常复杂的。例如,创建一个购物车,那并不是一个简单的事任务。然而,你可以将这个大任务分成几个小任务。这些小任务可能仅仅包括添加和删除的操作,而添加和删除的操作还可以被分解成更小的任务。你可以看到,不管任何任务,无论它有多复杂,只要可以分解,分解后的任务简单到不需要再对它进行分解了,你就可以实现这个任务。当你创建更复杂的程序时,你就会对这个过程有更加细致的体会了。

3 写一个基本的PHP程序

在我们开始写一个实际程序的时候,要看看创建程序的步骤。这些步骤并不复杂;实际上,这个过程和你创建一个HTML页面,并将它同步到你的服务器上的过程基本一致。

和创建一个HTML页面不同,创建一个PHP程序,你要面对的是文件中的代码,而不是那些所见即所得(WYSIWYG)的内容。如果你要利用这些所见即所得的编辑器开发程序(如微软的FrontPage,Macromedia Dream-Weaver,微软Word等),你可能还要花费一些时间来研究代码。

但令人兴奋的是,没有人也没有任何理由要求你,不能使用所见即所得的编辑器来编辑程序。然而,你可能会失望的发现,许多所见即所得的编辑器会修改或删除PHP文件中的代码。积于这个原因,分析你的编辑器是如何处理PHP代码的,这很重要。如果你想用你的编辑器来进行这种测试,要先创建一个新文件,保存,后缀名为.php。然后,切换到编辑器的源码视图,或在一个独立的其它编辑器中打开这个文件(如记事本等),输入接下来本文要引用的代码,并确保没有任何错误。

当你输入完成后,保存文件并切换回WYSIWYG编辑器视图。如果你看到你的PHP代码,不用管它,继续键入几行文字。如果你想添加一些Web页面中常用的元素,如

4

表格和图片等,就请继续插入这些内容,保存,并关闭WYSIWYG编辑器。

现在,在记事本中打开该文件,看看你的PHP代码是否发生了变化,包括代码格式上的改变,特殊字符的转换(如< <;)以及完全被删除的代码。

你可能会发现PHP代码被以某种方式改变了。因为PHP代码对一些WYSIWYG编辑器导致的变化是敏感的,如果你在写PHP代码的时候,使用WYSIWYG编辑器,这就很难让人理解了。PHP社区不会因为这个原因,而建议你不要使用WYSIWYG编辑器,因为使用这样的编辑器有时还是可以加快程序的开发速度的。

现在,当你读代码或者测试本文中的程序时,请试着习惯使用这些纯文本编辑器吧。当你习惯使用纯文本编辑器的时候,再使用其它的编辑器,那就容易上手的多了。到那时,你就可以识别出编辑器自身生成的代码,也就能更容易的找出提高你工作效率的方法了。

不管你现在处理PHP代码的编辑器是什么,如果你正在用的是WYSIWYG编辑器,我建议你使用如记事本这样的纯文本编辑器,或者一些带有语法加亮功能的纯文本编辑器。这些编辑器可以保证你的代码就像刚输入时的一样(WYSIWYG编辑器倾向于以它自己的规则来格式化你的代码,而这种功能对于编辑PHP代码来说并不需要)。即使你的编辑器没有改变你的PHP代码,但如果它不是一个严格基于文本的编辑器,仍然会在随后的运行中可能会出现问题。

这是创建和浏览HTML文件的一般过程:

1.创建HTML文件(添加文本,表格,图像,或声音等)。 2.保存HTML文件,命名为filename.html。 3.用FTP程序把你的文件上传到Web服务器。

4.用你的浏览器访问Web服务器上的文件(如,http:/ / www.example.com /filename.html)。

创建PHP程序的过程大致与创建HTML文件的过程相同:

1.创建HTML文件(包含文字,表格,图像,或声音),并在适当的位置插入PHP代码。

2.保存PHP文件,命名为filename.php。 3.使用FTP程序上传你的文件到Web服务器。

4.用你的浏览器访问Web服务器上的文件(如http:/ / www.example.com

5

/filename.php)。

可见,创建PHP程序和创建常规HTML程序的过程没有多大不同。

在了解了创建PHP程序的大致过程后,请看一下你的第一个PHP程序。看过代码你将了解,如何将PHP文件与普通的HTML文件相区分,如何把它上传到你的Web服务器上,以及在浏览器中页面看起来应该是什么样的。

PHP

PHP

/* Display a text message */

echo “Hello, world! This is my first PHP program.”; ?>

这个文件看起来和一个普通的HTML文件很像。注意,文件中包含了典型的HTML标签。事实上,如果你忽略标签间的内容,你也可以把该文件重新命名为.html的后缀名。

然而,这个文件的确包含了PHP代码,所以它必须被命名为.php后缀名。PHP代码在两个PHP标签之间(()),如图1.1所示。PHP标签之间的命令是“echo”(PHP的关键字,意思是“显示文本”)随后跟的是要显示的文本。结果很快被显示出来,“echo”后面的文本看起来就像是包含在HTML文件本身一样,似乎就没有PHP代码存在过。

6

图1.1:这幅图显示了一个基本的PHP程序的不同部分。

在看输出的结果之前,我们先把这个文件上传到Web服务器上并且运行。按照上面写PHP程序的过程,将它保存为PHP文件(.php后缀名),并上传到你的Web服务器。

一旦你的程序被上传到您的Web服务器,在你的浏览器中输入该文件地址。你应该得到一个返回的页面,它看起来应该非常类似如下的截图。

图1.2:当你访问你的程序时应该看到这样的页面。

4 编程语法

当你访问刚刚上传的PHP程序时,该PHP程序在返回浏览器之前是经历了一个复杂的过程的。PHP文件中的相关命令就是在这个过程中被执行的;在这个例子中,是一个单独的echo语句。图1.3显示当PHP文件发送请求时历经的过程。

7

图1.3:不像HTML文件,PHP文件在被返回浏览器时执行了文件中包含的PHP命令。

PHP解释器(或解析器)程序,正如上图中看到的那样。它读取PHP程序并执行它所能理解的命令。(如果PHP碰巧遇到一个它不能理解的命令,它会停止继续解析该文件,并返回一个错误消息给浏览器。)

每当PHP在给Web服务器发送请求时,该PHP解释器在给浏览器反馈信息前,必须先处理PHP文件中的命令。因为PHP的每次运行,都必须调用相关的例程,以便执行这些命令。

这和编译型语言有所不同,如C或C + +,它们只被解释一次;C语言程序,直接被翻译成机器码(机器码直接由CPU处理)。PHP不得不解释程序中包含的命令,这些命令还在必须以PHP能够识别的形式给出。例如,如果有人走向你,用德语询问时间,你可能不知道他在说什么(除非你懂得德语或者这个人指着他的手腕)。同样地,如果我走到你面前说,用英文语美味问你,“现在几点了?“你可能也不知道我在说什么。

PHP也有类似的限制。语句(命令的集合,由特定字符组成,PHP能够识别的句子)必须以正确的命令格式给出。例如,你要在PHP中显示一条文本,你必须使用PHP能够识别的命令,如“echo”。同时,你必须把你的单词,以正确的顺序,用英语讲给别人。还要格式化要传递给PHP执行的语句,以便PHP解释器能够识别并执行。

语法,就是将PHP能够理解并执行的句子组织到一起的过程中要遵循的规则,这

8

就是PHP的开始和结束标签。PHP只解析在PHP标签之间的代码。而不在这其中的代码只是作为HTML页面的一部分返回,正如先前看到例子一样。

如下这个例子,该语句不能执行,即使该命令是PHP语言的命令: echo “This won’t work.”

该语句不会执行,因为它没有遵循所有的语句必须以分号结尾的基本语法规则。一些特殊的语句不需要有分号,但不是很多(如do语句。)注意,PHP语句必须以分号结束。下面的语句能够正确执行并输出:

echo “This works!”;

你可能注意到了,即使两个语句在同一行,但语句间并没有分号分隔,PHP并不会报错。这是PHP的一个特性,它使得插入一个单独的echo语句变得更加容易。如果你想在第一个echo语句运行后看到返回的错误提示,那么将这两个语句输出到不同的行,看起来就像这个样子:

echo “This won’t work.” echo “This won’t work.”

代码不会运行,PHP将返回一个错误信息,因为没有分号来分隔这两个语句。

5 嵌入式语言如何工作

通过前面的讲解,我们知道PHP代码必须封装在这两个 PHP标签之间。在同一文件中,使用标签来分隔PHP代码和HTML代码,这就允许将PHP代码要处理的信息和HTML要显示的信息直接混合在一起。这使得PHP成为一种嵌入式编程语言,因为PHP代码被直接嵌入在HTML代码中了。

这个概念是相对较新的:在PHP语言出现之间,并没有程序要像HTML这样复杂的格式化显示的需求。显示在屏幕上的信息通常只是字母,数字,和空格,没有颜色,大小或其它的格式标记。

自从PHP作为Web编程语言出现以来,它便趋向于在HTML中使用,这大大增加了要返回给浏览器的信息量。PHP不仅要返回信息给用户,而且也需要正确的格式化这些信息。

为了使信息和标签能够简单混合,PHP代码被直接嵌入在HTML页面中。开篇的

9

例子比较清楚的演示了这一概念;程序里大部分是常规的HTML代码,但PHP也会插入一些适当的信息到页面中。

嵌入式编程,将使程序员的工作更为便捷;你可以在任何你想要插入代码的地方插入PHP代码,剩下的地方就写常规的HTML代码了。然而,别忘了用PHP标签封装你的PHP代码,否则你的代码将不能被解析,而是会被直接显示在HTML页面上。

下面的程序演示了一个嵌入式编程的例子:

/* File: hello_world.php – displays “Hello, World!” */ ?>

Hello, World! Hello,

// Send “World!” to the visitor?s browser echo “World!”; ?>

当Web服务器访问这个文件的时候,PHP解释器将会从头到尾的逐行执行文件中的命令。因此, PHP标签间处理的信息就随着echo语句一起返回给浏览器了。Web浏览器接收到的内容和如下代码所示效果相同:

Hello, World! Hello, World!

然后浏览器就可以像显示其它HTML文件一样显示“Hello World!”信息了。

10

6 服务端和客户端脚本

就像先前解释的一样,在信息返回浏览器之前,PHP代码要在Web服务器端执行,这被称为服务器端处理。大多数的Web编程都是以这种形式存在的:像ASP、PHP、Perl、C等。

然而,还有一些语言是在浏览器接收到返回的页面时进行处理的。这就是所谓的客户端处理。最常见的例子就是JavaScript。

这可能会导致逻辑上的一个有趣的问题。下面的示例演示了这种情况:

多数情况下,这么写代码的程序员通常希望只有一个echo语句被执行。然而,这两句都将执行,页面将停留在生成错误的JavaScript页面(因为echo语句中的信息不是有效的JavaScript代码)。如果你还不是很清楚,继续看下面的例子;如下的代码会让你更加明确。这段代码是从以前的代码片段中摘录下来的,注意JavaScript是原封不动的,但PHP代码已经被调用了。PHP完全忽略了JavaScript代码:

正如你所看到的,这会导致JavaScript执行时的错误。将PHP和JavaScript代码混合使用要小心:可以这样做,但是要注意,PHP将永远无视JavaScript的存在。为了成功地将二者结合起来,用PHP输出JavaScript代码是非常必要的。

下面的例子就是这么做的:

7 运行你的程序

按照本文开始介绍的过程,尝试运行这个程序。 你将在你的浏览器里看到 “Hello World!“。

12

1 Why PHP?

PHP is an excellent choice for Web programming. It has many advantages over other languages, including other Web-oriented languages. To get a very general understanding of how the common Web programming languages compare, let?s compare them.

ASP is Microsoft?s Web programming environment. (It?s not a language itself because it allows the programmer to choose from a few actual languages, such as VBScript or JScript.) ASP is simple, but too simple for programs that use complex logic or algorithms.

Besides ASP?s over-simplicity, many companies find it hard to budget for the expense of Microsoft licenses. Without even considering hardware costs, a Microsoft server could cost thousands of dollars in licensing, whereas a comparable Unix-based operating system running PHP could be free.

Another language well known for its use on the Web is Sun Microsystems? Java. Java is praised for being platform-independent (a program written in Java can be run on virtually any computer without having to make any modifications to the program).

Although Java does have its advantages, it has serious downsides in development time, development cost, and execution speed. Java development is time-consuming because projects in Java must follow strict rules (imposed by Java) that require extensive planning. In addition to high development time, the cost is also high because Java developers are expensive to hire. The cost is therefore potentially much higher than it would be if the project were done in another language. Even after the project is built, a program written in Java takes longer to run than one written in one of the other languages to which we?re comparing.

Overall, when compared to Java, PHP comes out with flying colors. It is not

13

unheard of for a Java project to take two or three times the time to develop compared to a similar project in PHP. On top of that, the final program runs on a wide array of platforms (like Java), except the PHP program runs faster.

Another language commonly used for writing Web programs is Perl. Perl, like PHP, is an open-source project developed to run on many platforms. In fact, Perl has been around longer than PHP. Before PHP, Perl was generally accepted as the best Web programming language. However, during the past few years, PHP has earned a reputation for being better than Perl for Web programming because PHP provides a vast number of features as part of PHP itself, whereas you would have to download separate modules to get the same functionality in Perl. This leads to problems when programs are transferred from one system to another because the modules have to be downloaded from Perl?s exhaustive (and confusing) module archive known as CPAN.

The last language to compare PHP to is C. C has been around for a long time; it has been used in a variety of computers, from mainframes to consumer PCs. The problems creating a Web program in C are obvious if you know C. To develop a Web program in C, you have to develop all of the basic functionality of Web programming (such as collecting the data from HTML forms) before you can even begin to think about the actual task at hand. Since PHP provides for all the common (and many uncommon) Web programming tasks, writing such a program in PHP allows the programmer to get straight to the point.

You could write volumes on PHP?s advantages over other programming languages when it comes to Web programming. There are many, many articles on the Internet comparing PHP to Java, Perl, ASP, and others. Once you?ve earned some experience programming in PHP, you might find yourself trying to convince your client or employer to allow you to use it instead of another language. If that problem arises, you should find plenty of helpful information by doing a Web search.

PHP has an unlimited number of uses. The original version was used solely to track who was viewing the creator?s resume. Over time, however, that simple

14

tracking program evolved into a language of its own.

PHP?s primary use certainly isn?t to track resumes anymore; it has grown to be able to do that and just about anything else. To give you a better idea of what PHP can do, here are some of its common uses:

? Feedback forms

? Shopping carts and other types of e-commerce systems

? User registration, access control, and management for online subscription services

? Guest books

? Discussion and message boards

2 If You Are New to Programming

If you?ve never written a computer program before, the whole idea may be quite intimidating. Most programmers will probably tell you (if they aren?t embarrassed to admit it) that they were intimidated when they began. However, the programming process isn?t all that difficult and, contrary to popular belief, you don?t have to have an extremely high IQ to be good at it.

When you write a program, your main goal is to translate your idea into a language that the computer can understand. For example, if you were teaching a person how to cook hamburgers, you would first describe the process of forming the ground beef into patties. Then, you would tell the person how to put the burgers on the grill, how long to leave them there, and finally how to remove them.

Of course, just because you can describe the process of making hamburgers doesn?t mean PHP is going to be cooking anything for you anytime soon. The point is, if you can describe a process like I just described making hamburgers, you can write a program.

Writing a PHP program is simply the process of describing to PHP how to do something. By the time you?ve finished reading this article, you will understand

15

all the concepts behind writing a PHP program. Those concepts are like the words and sentences used to describe hamburgers. The more you read this article, the more “words” you will understand, and the better you will be able to “describe” your task to PHP. Thus, you will learn to write PHP programs to suit whatever need or idea you have, and soon it won?t be any more intimidating than telling someone how to cook hamburgers.

Some programming problems might be very complex when examined as a whole. For example, creating a shopping cart is definitely not a simple task. However, a shopping cart can be broken into a few smaller tasks. Those tasks might include adding and removing items, which are both tasks that can break into even smaller tasks. You will find that any task, no matter how complex, can be broken into smaller ones until each task is simple enough that breaking it down further is unnecessary. This process is explained in more detail when you begin creating programs with more complexity

3

Writing a Basic PHP Program

Before we get into an actual program, let?s take a look at the steps we?ll take to create one. The steps aren?t complicated; in fact, they?re basically the same as the steps you use when creating an HTML page and publishing it to your server.

Unlike creating an HTML page, creating a PHP program requires that you actually work with the source code of the file as opposed to a “what you see is what you get” (WYSIWYG) approach. If you?re used to using a WYSIWYG program (such as Microsoft FrontPage, Macromedia Dream-Weaver, or Microsoft Word), it may take you some time to get used to looking at the source code.

The good news is there?s no reason that you can?t continue to use a WYSIWYG editor to create an HTML design for your program. However, you may be disappointed to find that many WYSIWYG editors mangle or even delete vital PHP code from your files. For this reason, it is important to find out how your particular editor handles PHP code. If you want to test your WYSIWYG to

16

see how it handles PHP code, create a new file, naming it with a .php extension. Then, switch to your editor?s source view or open the file in a separate program, such as Notepad and enter the program shown in the first example later in the chapter, making sure not to make any mistakes.

When you?re finished, save the file and switch back to the WYSIWYG editor. If you see your PHP code, work around it and type a few lines of text. If you want, add some common elements that you include in your Web pages, such as tables and images. Save the file again and close all the open editors.

Now, open the file in Notepad and look at the PHP code. Look for any changes, including changes in the way the code is formatted, special characters that have been converted into codes (such as < to <), and code that has been completely removed.

You will probably find that the PHP code has been changed in some way. Because PHP is sensitive to some of the changes a WYSIWYG editor might make, it?s almost impossible to use a WYSIWYG editor once you?ve started adding PHP code. The PHP community won?t tell you that using a WYSIWYG editor is a sign of weakness; doing so can speed things up a lot sometimes.

For now, try using a plain-text editor when you?re reading and experimenting with the examples in this article. When you?re comfortable with that, feel free to try it with whatever editor you want. By that time, you?ll be able to recognize code that the editor has mangled, and you?ll have an easier time finding what works best for you.

Regardless of how your current editor handles PHP code, if you are using a WYSIWYG editor, I suggest that you use an editor such as Notepad or one of the many free syntax-highlighting editors out there. Using one of these programs will ensure that your code stays just as you typed it (WYSIWYG editors tend to reformat things as they see fit, which isn?t desirable when coding PHP). Even if your editor passed the test, if it?s not a strictly text based (not WYSIWYG) editor, you might find yourself running into problems later.

Here is the process you might use in creating and viewing an HTML file:

17

1. Create your HTML file (add text, tables, images, or sounds). 2. Save your HTML file as filename.html.

3. Use an FTP program to upload your file to the Web server.

4. Point your browser to the address of the file on your Web server (for example, http://www.example.com/filename.html).

The process you would use to create a PHP program is much the same: 1. Create your HTML file (containing text, tables, images, or sounds) and insert PHP code where desired. 2. Save your PHP file as filename.php.

3. Use an FTP program to upload your file to the Web server.

4. Point your browser to the address of the file on your Web server (such as http://www.example.com/filename.php).

The process of creating a PHP program isn?t much different from the process you follow to create a regular HTML page.

Now that you?ve seen the overall process, let?s take a look at our first PHP program. After reading the following example, you?ll learn what separates it from a normal HTML file, how to upload it to your Web server, and what the page should look like viewed in your browser.

PHP

PHP

/* Display a text message */

echo “Hello, world! This is my first PHP program.”; ?>

This file looks a lot like a regular HTML file. Notice that the file has HTML

18

tags typical of those you would find in any HTML file. In fact, if you disregard everything between the tags, you might as well rename this file with an .html extension.

However, this file does contain PHP code, so it must be named with a .php extension. The PHP code lies between the PHP tags () as shown in Figure 1.1. The command between the PHP tags is echo (PHP?s word for “add the following text to the page”) followed by the text to display. The output, which will be shown soon, looks just as if the text after echo had been in an HTML file itself and no PHP code ever existed.

Figure 1.1: This diagram shows the different parts of a basic PHP program. Before we look at the output, let?s upload this file to a Web server and run it. Follow the process outlined previously to write the program, save it as a PHP file (with a .php extension), and upload it to your Web server.

Once your program is uploaded to your Web server, type its address into your browser. You should get a page back that looks very similar to the screenshot in Figure 1.2.

19

snippet follows; notice that the JavaScript has been left intact and untouched, but the PHP code has been evaluated. PHP ignores the JavaScript code completely:

As you can see, this code will cause JavaScript errors when executed. Be cautious when combining PHP and JavaScript code: It can be done, but it must be done with attention to the fact that the PHP will always be evaluated without regard for the JavaScript. To successfully combine the two, it?s generally necessary to output JavaScript code with PHP.

The following example does just that:

25

7 Running Your New Program

Following the same procedure outlined at the beginning of this article, try running this program.

Your program display “Hello, World!” in your browser.

26

本文来源:https://www.bwwdw.com/article/idc6.html

Top