例子比较清楚的演示了这一概念;程序里大部分是常规的HTML代码,但PHP也会插入一些适当的信息到页面中。
嵌入式编程,将使程序员的工作更为便捷;你可以在任何你想要插入代码的地方插入PHP代码,剩下的地方就写常规的HTML代码了。然而,别忘了用PHP标签封装你的PHP代码,否则你的代码将不能被解析,而是会被直接显示在HTML页面上。
下面的程序演示了一个嵌入式编程的例子:
/* File: hello_world.php – displays “Hello, World!” */ ?>
// Send “World!” to the visitor?s browser echo “World!”; ?>
当Web服务器访问这个文件的时候,PHP解释器将会从头到尾的逐行执行文件中的命令。因此, PHP标签间处理的信息就随着echo语句一起返回给浏览器了。Web浏览器接收到的内容和如下代码所示效果相同:
然后浏览器就可以像显示其它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