[Previous] [Contents] [Next]

Introducing PHP

The current version of PHP is PHP4, which we call PHP throughout this tutorial. The current release at the time of writing is 4.0.6.

PHP is a recursive acronym that stands for PHP: Hypertext Preprocessor; this is in the naming style of GNU, which stands for GNU's Not Unix and which began this odd trend. The name isn't a particularly good description of what PHP is and what it's commonly used for. PHP is a scripting language that's usually embedded or combined with HTML and has many excellent libraries that provide fast, customized access to DBMSs. It's an ideal tool for developing application logic in the middle tier of a three-tier application.

PHP Basics

Example 2-1 shows the first PHP script in this tutorial, the ubiquitous "Hello, world." When requested by a web browser, the script is run on the web server and the resulting HTML document sent back to the browser and rendered as shown in Figure 2-1.

Figure 2-1. The rendered output of Example 2-1 shown in the Netscape browser
figs/wda_0201.gif
Example 2-1. The ubiquitous Hello, world in PHP
<!DOCTYPE HTML PUBLIC
   "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
  <title>Hello, world</title>
</head>
<body bgcolor="#ffffff">
  <h1>
  <?php
    echo "Hello, world";
  ?>
  </h1>
</body>
</html>

Example 2-1 illustrates the basic features of a PHP script. It's a mixture of HTML-in this case it's mostly HTML-and a PHP script. The script in this example:

<?php
  echo "Hello, world";
?>

simply prints the greeting, "Hello, world."

The PHP script shown in Example 2-1 is rather pointless: we could simply have authored the HTML to include the greeting directly. Because PHP integrates so well with HTML, using PHP to produce static strings is far less complicated and less interesting than using other high-level languages. However, the example does illustrate several features of PHP:

  • The begin and end script tags are <?php and ?> or, more simply, just <? and ?>. The longer begin tag style <?php avoids conflicts with other processing instructions that can be used in HTML.

    Other begin and end tag styles can also be configured, such as the HTML style that is used with JavaScript or other embedded scripts: <script language="PHP"> and </script>.

  • Whitespace has no effect, except to aid readability for the developer. For example, the script could have been written succinctly as <?php echo "Hello, world";?> with the same effect. Any mix of spaces, tabs, carriage returns, and so on in separating statements is allowed.

  • A PHP script is a series of statements, each terminated with a semicolon. Our simple example has only one statement: echo "Hello, world";.

  • A PHP script can be anywhere in a file and interleaved with any HTML fragment. While Example 2-1 contains only one script, there can be any number of PHP scripts in a file.

  • When a PHP script is run, the entire script including the start and end script tags <?php and ?> is replaced with the output of the script.

When we present a few lines of code that are sections of larger scripts, we usually omit the start and end tags.

The freedom to interleave any number of scripts with HTML is one of the most powerful features of PHP. A short example is shown in Example 2-2; a variable, $outputString="Hello, world", is initialized before the start of the HTML document, and later this string variable is output twice, as part of the <title> and <body> elements. We discuss more about variables and how to use them later in this chapter.

Example 2-2. Embedding three scripts in a single document
<?php $outputString = "Hello, world"; ?>
<!DOCTYPE HTML PUBLIC
   "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
  <title><?php echo $outputString; ?></title>
</head>
<body bgcolor="#ffffff">
  <h1><?php echo $outputString; ?></h1>
</body>
</html>

The flexibility to add multiple scripts to HTML can also lead to unwieldy, hard-to-maintain code. Care should be taken in modularizing code and HTML; we discuss how to separate code and HTML using templates in Chapter 13.

[Previous] [Contents] [Next]