Page 65 - Beginning PHP 5.3
P. 65

Chapter 2: Your First PHP Script
                         How It Works
                         This example shows how you can embed PHP within an HTML page. The PHP code itself is exactly
                         the same — echo “Hello, world!” — but by surrounding the PHP with HTML markup, you’ve
                         created a well-formed HTML page styled with CSS.
                         First, a DOCTYPE and the opening html tag are used to declare that the page is an XHTML 1.0 Strict
                         Web page:
                             <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                               “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
                             <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
                         Next, the head element of the Web page gives the page a title — “Hello” — and links to a style sheet file,
                         common.css:
                             <head>
                             <title>Hello</title>
                             <link rel=”stylesheet” type=”text/css” href=”common.css” />
                             </head>

                         Finally, the body element includes an h1 (top-level heading) element containing the output from the PHP
                          code — echo “Hello, world!”; — and the page is then finished with a closing html tag:

                             <body>
                             <h1><?php echo “Hello, world!”; ?></h1>
                             </body>
                             </html>
                             Note that you don’t need to have the <?php and ?> tags on separate lines. In this case, the tags and
                             enclosed PHP code are all part of a single line.
                          Meanwhile, the common.css style sheet file contains selectors to style some common HTML
                          elements — including the h1 heading used in the page — to produce the nicer-looking result.


                               Keep this common.css file in your document root folder, because it’s used
                               throughout other examples in this book.


                         If you view the source of the resulting Web page in your browser, you can see that the final page is
                         identical to the original HTML markup, except that the PHP code —  <?php echo “Hello, world!”;
                         ?> — has been replaced with the code’s output (“Hello, world!”). The PHP engine only touches the parts
                          of the page that are enclosed by the <?php ... ?> tags.













                                                                                                          27





                                                                                                      9/21/09   8:50:26 AM
          c02.indd   27
          c02.indd   27                                                                               9/21/09   8:50:26 AM
   60   61   62   63   64   65   66   67   68   69   70