If you’re familiar with any programming language, PHP should be easy to learn. If you have done no programming before, the pace of this chapter may be brisk but should still be manageable. PHP has a syntax similar to JavaScript, which many web designers have learned; both languages hark back to the classic C and Perl languages in syntax. The topics covered in this chapter include:
We conclude the chapter with a short example that puts many of the basic PHP concepts together. Introducing PHPThe current version of PHP is PHP4 (Version 4.3.4). PHP5 is available for beta testing at the time of writing as Version 5.0.0b3. We discuss both versions in this chapter. 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 the HTML of a web page. When the page is requested, the web server executes the PHP script and substitutes in the result back into the page. PHP has many excellent libraries that provide fast, customized access to DBMSs and is an ideal tool for developing application logic in the middle tier of a three-tier application. PHP BasicsExample 2-1 shows the first PHP script in this book, the ubiquitous “Hello, world.” It’s actually mostly HTML; the PHP is embedded near the end. Example 2-1. The ubiquitous Hello, world in PHP <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 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 output of Example 2-1 shown in the Netscape browser 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 PHP code. The PHP code in this example: <?php 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 sequence of characters is far less complicated and less interesting than using other high-level languages. However, the example does illustrate several features of PHP:
The point of learning PHP, of course, is to create pages that change, pages that contain dynamic content derived from user input or a database. The first step toward that goal is to introduce a variable, which is something that can change from run to run. In this chapter, we don’t use dynamic content. But we can show how to set a variable to a string as follows: <?php $outputString = "Hello, world"; ?> And then rewrite our script as follows: <?php print $outputString; ?> Because$outputStringhas been set toHello, world, that string is printed as part of the surrounding HTML page. The freedom to interleave blocks of PHP statements with HTML is one of the most powerful features of PHP. A short example is shown in Example 2-2; the variable$outputStringis initialized before the start of the HTML document, and later this 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 blocks of code in a single document <?php $outputString = "Hello, world"; ?> The flexibility to add multiple blocks of PHP 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 7.
blog comments powered by Disqus |
|
|
|
|
|
|
|