A Working Example

In this section, we use the techniques described so far to develop a simple, complete PHP script. The script doesn’t process input from the user, so we leave some of the best features of PHP as a web scripting language for discussion in later chapters.

Our example is a script that produces a web page containing the times tables. Our aim is to output the 1-12 times tables. The first table is shown in Figure 2-2 as rendered by a Netscape browser.

The output of the times-tables script shown rendered in a Netscape browser

Figure 2-2. The output of the times-tables script shown rendered in a Netscape browser

To begin the development, we need to design how the output should appear and, therefore, what HTML needs to be produced. If we use simple HTML markup, the first 12 lines of the HTML produces Example 2-3 as follows:

<html>
<head>
  <title>The Times-Tables</title>
</head>
<body bgcolor="#ffffff">
<h1>The Times Tables</h1>
<p><b>The 1 Times Table</b>
<br>1 x 1 = 1
<br><b>2 x 1 = 2</b>
<br>3 x 1 = 3
<br><b>4 x 1 = 4</b>
<br>5 x 1 = 5

The script produces this output using a mixture of HTML and an embedded PHP script.

The completed PHP script and HTML to produce the times tables are shown in Example 2-3. The first nine lines are HTML that produces the <head> components and the <h1>The Times Tables</h1> heading at the top of the web page. Similarly, the last two lines are HTML that finishes the document: </body> and </html>.

Between the two HTML fragments ...

Get Web Database Applications with PHP, and MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.