Adding PHP to Your Pages

Adding PHP to Your Pages

All this HTML is nice, but presumably you're here to learn PHP, so it's high time to add PHP code to a page. PHP can be used to add characteristics to your page that aren't typically possible with normal HTML and CSS.

Ensuring That Your Server Supports PHP

A page written in PHP can be identical to an HTML page. Both are written with a plain text editor, and stored on a Web server. A PHP program can have <script> elements embedded in the page. When the user requests a PHP page, the server first examines the page and executes any script elements before it sends the resulting HTML to the user. This will only work if the Web server has been configured to use the PHP language. You might need to check with your server administrator to see if this support is available. On a home computer, you can use the PHP Tripod software included on the CD-ROM that accompanies this book to set up all the necessary components.


To run all the programs in this book, your server needs to have three different components installed. First, you will need a Web server such as Microsoft IIS or Apache. Secondly, you'll need the PHP interpreter, which is a program that reads PHP files and converts them into HTML pages. Finally, you'll need a database management program to handle data. PHP Triad integrates all these features into one installation. It includes the Apache (free and very powerful) Web server, the PHP interpreter, and the mySQL database management system. This package is very typical of most servers that use PHP. If the Web host you are using does not yet support PHP, you can still install the programs and practice on your own machine (although nobody outside your computer will be able to get to your programs).

Adding PHP Commands to an HTML Page

The easiest way to determine if PHP exists on your server is to write a simple PHP program and see if it works. Here's a very simple PHP program.

<title>Hello in PHP</title>
<h1>Hello in PHP</h1>

print "Hello, world!";



The <? ?> sequence is the easiest way to indicate PHP code, but it isn't always the best way. You can also indicate PHP code with a longer version like this: <?php ?>. This version works better when your code will be interpreted as XML. You can also specify your code with normal HTML tags just like JavaScript <script language = "php"></script>. Some PHP servers are configured to prefer one type of script tag over another so you may need to be flexible. However, all these variations work in exactly the same way.

A PHP program looks a lot like a typical HTML page. The only thing that's different is the special <? ?> tag. This tag specifies the existence of PHP code. Any code inside the tag will be read by the PHP interpreter, then converted into HTML code. The code written between the <? and ?> symbols is PHP code. I added two commands to the page. Look at the output of the program shown in Figure 1.12, and you might be surprised:

Click To expand
Figure 1.12: The page mixes HTML with some other things.

Examining the Results

There are three distinct types of text on this page. First, the "Hello in PHP" line is ordinary HTML. I wrote it just like a regular HTML page, and it was displayed just like regular HTML. The "Hello world" line is a little different though, because it was written by the PHP program embedded in the page. The rest of the page is a bit mysterious. It contains a lot of information about the particular PHP engine being used. It actually stretches on for several pages. All that code was generated by the phpInfo() command. This command is used to display information about the PHP installation. It isn't that important to understand all the information displayed by the phpInfo() command. It's much more critical to appreciate that when the user requests the hello.html Web page, the text of the page is first run through the PHP interpreter. This program scans for any PHP commands, executes the commands, and prints HTML code in place of the original commands. By the time a page gets to the user, all the PHP code is gone, because the server used the PHP to generate HTML code. For proof of this, point your browser at hello.php and then view the source code. It will look something like this:

<title>Hello in PHP</title>
<h1>Hello in PHP</h1>

Hello, world!<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head><style type="text/css"><!--
a { text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-family: arial, helvetica, sans-serif; font-size: 18pt; font-weight: bold;}
h2 { font-family: arial, helvetica, sans-serif; font-size: 14pt; font-weight: bold;}
body, td { font-family: arial, helvetica, sans-serif; font-size: 10pt; }
th { font-family: arial, helvetica, sans-serif; font-size: 11pt; font-weight: bold; }
<title>phpinfo()</title></head><body><table border="0" cellpadding="3" cellspacing="1"
width="600" bgcolor="#000000" align="center">
<tr valign="middle" bgcolor="#9999cc"><td align="left">
<a href=""><img src="/phab/ph01/hello.php?=PHPE9568F34-D428-11d2-A769-
00AA001ACF42" border=0 align="right" alt="PHP Logo"></a><h1>PHP Version 4.2.1</h1>

Note that I showed only a small part of the code generated by the phpInfo() command, and that the details of the code might be different when you run the program on your own machine. The key point here is the PHP code to write "Hello World!" (print "Hello World!") is replaced with the actual text "Hello World!" More significantly, the very simple phpInfo() command is replaced by a huge amount of HTML code.

A small amount of PHP code can very efficiently generate large and complex HTML documents. This is one significant advantage of PHP. Also, by the time the document gets to the Web browser, it's plain vanilla HTML code, which can be read easily by any browser. These two features are important benefits of server-side programming in general, and of PHP programming in particular. As you progress through this book, you'll learn about many more commands for producing interesting HTML, but the basic concept is always the same. Your PHP program is simply an HTML page that contains special PHP markup. The PHP code is examined by a special program on the server, and the results are embedded into the Web page before it is sent to the user.