Page 63 - Beginning PHP 5.3
P. 63

Chapter 2: Your First PHP Script
                           This script is very simple, but let ’ s break it down line - by - line to explore a few features of PHP.

                           The first line tells the PHP engine to expect some PHP code to follow:

                                 < ?php

                           Why do you need this line? This is due to the fact that PHP can be embedded within HTML Web pages.
                         When the PHP engine first starts processing the page, it assumes it ’ s dealing with plain old HTML until
                         told otherwise. By using the PHP delimiter,  < ?php, you ’ re telling the PHP engine to treat anything
                         following the   < ?php as PHP code, rather than as HTML.

                            The next line displays the message  “ Hello, world! ” :


                               echo “Hello, world!”;
                          PHP ’ s echo() statement takes a string of text  —  in this case, “Hello, world!”  —  and sends it as part
                          of the Web page to the browser. The browser then displays the  “ Hello, world! ”  text to the visitor. Notice
                          the semicolon (;) at the end of the line; this tells PHP that you ’ ve reached the end of the current
                          statement, and it should look for a new statement (or the end of the PHP code) to follow.
                               echo() doesn ’ t have to be given a string of text; it can display anything that can be displayed, such as num-
                             bers or the results of expressions. You find out more about data types and expressions in the next chapter.
                               An alternative to echo() is the print() statement, which works in exactly the same way except that
                             it also returns a value (true). Generally speaking, you can use print() instead of echo() in your
                             code if you prefer.

                            The final line of your simple script tells the PHP engine that it ’ s reached the end of the current section of
                          PHP code, and that the following lines (if any) contain plain HTML again:

                              ? >


                           Embedding  PHP  within  HTML

                           As you ’ ve gathered by now, one of the nice things about PHP is that you can embed PHP code within
                         HTML. In fact, each .php script that you write is essentially treated as an HTML page by default. If the
                         page contains no  < ?php ... ? >  tags, the PHP engine just sends the contents of the file as - is to the browser.


                       Try It Out     Creating a Stylish Page
                         You can use this feature to make your “Hello, world!” example prettier by adding a proper HTML
                         header and footer and including a CSS style sheet. Enter the following code and save it as hello_
                         pretty.php in your document root folder:
                             <!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”>
                              <head>
                               <title>Hello</title>
                               <link rel=”stylesheet” type=”text/css” href=”common.css” />
                              </head>


                                                                                                          25





                                                                                                      9/21/09   8:50:25 AM
          c02.indd   25
          c02.indd   25                                                                               9/21/09   8:50:25 AM
   58   59   60   61   62   63   64   65   66   67   68