Page 67 - Beginning PHP 5.3
P. 67

Chapter 2: Your First PHP Script
                           The majority of the code is exactly the same as before. The only difference is the PHP code itself:

                                 < ?php
                             $currentTime = date( “g:i:s a” );
                             echo “Hello, world! The current time is $currentTime”;

                             ? >
                           The first line of PHP code takes the current time and formats it as a readable string of text, then stores
                         this string of text in a variable called $currentTime. ( Variables  are containers that store data. You learn
                          all about them in the next chapter.) To format the time, the script uses the built - in date() function. The
                         string of characters between the quotation marks tells PHP how to format the time, as follows:

                            ❑       g, i, and s tell PHP to output the current hour, minute, and second, respectively
                            ❑       a tells PHP to display either  “ am ”  or  ‘ pm ”  as appropriate
                            ❑       The colons (:) and the space character are not processed by the date() function, so they ’ re
                                displayed as - is
                               You learn all the ins and outs of PHP ’ s date() function in Chapter 16.

                            Then the second line of code displays the  “ Hello, world! ”  message, including the current time. Reload
                          the page in your browser and you ’ ll see the time change.
                               Notice how PHP lets you include variable names within text strings, as is the case with the
                             $currentTime variable. This makes it easy for you to create text messages containing dynamic
                             information.

                            This simple example is the essence of a dynamic Web page  —  a page whose content is potentially
                          different each time the page is viewed. In this book you learn how to use PHP to add all sorts of dynamic
                          content to your sites.


                           Using Comments to Make Code More Readable

                           To round off this chapter, you learn about another basic feature of PHP:  comments . A comment is simply
                          text that is ignored by the PHP engine. The purpose of comments is to let you add messages to yourself
                          (and other programmers) that explain what your code does. It ’ s always a good idea to add comments to
                          your code, even if you ’ re the only programmer working on it. Sometimes code that makes sense when
                          you write it can seem as clear as mud in three months ’  time, so comments can really help!

                            PHP supports single - line comments and multi - line comments. To write a single - line comment, start the
                          line with either two slashes (//) or a hash symbol (#). For example:

                               // This code displays the current time
                             # This code displays the current time










                                                                                                          29





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