Page 66 - Beginning PHP 5.3
P. 66

Part I: Getting Up and Running with  PHP
                  Enhancing the Script Further

                   The  “ Hello, world! ”  example shows how you can write a simple PHP script, but the code does nothing
                 useful  —  you could just as easily achieve the same effect with a simple HTML page. In this section you
                 enhance the script to display the current time. In doing so, you move from creating a static Web page to a
                 dynamic page; a page that changes each time you view it.

                  Here ’ s the modified  “ Hello, world! ”  script:

                    <!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>
                      <body>
                        <h1>
                    <?php
                    $currentTime = date( “g:i:s a” );
                    echo “Hello, world! The current time is $currentTime”;
                    ?>
                        </h1>
                      </body>
                    </html>

                     Save the script as hello_with_time.php and open its URL in your browser to run it. You ’ ll see
                 something along the lines of Figure  2-14 .


















                             Figure 2-14


                      If, when running this script, you see a lengthy warning message telling you that it is not safe to rely on
                    the system’s time zone settings, you need to configure PHP ’ s time zone. See the  “ Setting Your Time
                    Zone ”  section earlier in the chapter for instructions.







              28





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