Before you go any further, you need to make sure you can create and run PHP scripts as you go through the examples in this book. This could be on your own machine, and you can find instructions for installing PHP in Appendix A, "Installing PHP." Also, many web hosting companies include PHP in their packages, and you may already have access to a suitable piece of web space.
Go ahead and create a new file called time.php that contains Listing 1.1, in a location that can be accessed by a PHP-enabled web server. This is a slight variation on the date example shown previously.
Listing 1.1. Displaying the System Date and Time
The time is <?php echo date('H:i:s');?> and the date is <?php echo date('j F Y');?>
When you enter the URL to this file in your web browser, you should see the current date and time, according to the system clock on your web server, displayed.
If you entered Listing 1.1 exactly as shown, you might notice that the actual output produced could be formatted a little betterthere is no space between the time and the word and. Any line in a script that only contains code inside PHP tags will not take up a line of output in the generated HTML.
If you use the View Source option in your web browser, you can see the exact output produced by your script, which should look similar to the following:
The time is 15:33:09and the date is 13 October 2004
If you insert a space character after ?>, that line now contains non-PHP elements, and the output is spaced correctly.
The echo Command
While PHP is great for embedding small, dynamic elements inside a web page, in fact the whole page could consist of a set of PHP instructions to generate the output if the entire script were enclosed in PHP tags.
The echo command is used to send output to the browser. Listing 1.1 uses echo to display the result of the date command, which returns a string that contains a formatted version of the current date. Listing 1.2 does the same thing but uses a series of echo commands in a single block of PHP code to display the date and time.
Listing 1.2. Using echo to Send Output to the Browser
<?php echo "The time is "; echo date('H:i:s'); echo " and the date is "; echo date('j F Y'); ?>
The non-dynamic text elements you want to output are contained in quotation marks. Either double quotes (as used in Listing 1.2) or single quotes (the same character used for an apostrophe) can be used to enclose text strings, although you will see an important difference between the two styles in Lesson 2, "Variables." The following statements are equally valid:
echo "The time is "; echo 'The time is ';
Notice that space characters are used in these statements inside the quotation marks to ensure that the output from date is spaced away from the surrounding text. In fact the output from Listing 1.2 is slightly different from that for Listing 1.1, but in a web browser you will need to use View Source to see the difference. The raw output from Listing 1.2 is as follows:
The time is 15:59:50 and the date is 13 October 2004
There are no line breaks in the page source produced this time. In a web browser, the output looks just the same as for Listing 1.1 because in HTML all whitespace, including carriage returns and multiple space or tab characters, is displayed as a single space in a rendered web page.
A newline character inside a PHP code block does not form part of the output. Line breaks can be used to format the code in a readable way, but several short commands could appear on the same line of code, or a long command could span several linesthat's why you use the semicolon to indicate the end of a command.
Listing 1.3 is identical to Listing 1.2 except that the formatting makes this script almost unreadable.
Listing 1.3. A Badly Formatted Script That Displays the Date and Time
<?php echo "The time is "; echo date('H:i:s'); echo " and the date is " ; echo date( 'j F Y' ); ?>
Another way to make sure your code remains readable is by adding comments to it. A comment is a piece of free text that can appear anywhere in a script and is completely ignored by PHP. The different comment styles supported by PHP are shown in Table 1.1.
Listing 1.4 produces the same formatted date and time as Listings 1.1, 1.2, and 1.3, but it contains an abundance of comments. Because the comments are just ignored by PHP, the output produced consists of only the date and time.
Listing 1.4. Using Comments in a Script
<?php /* time.php This script prints the current date and time in the web browser */ echo "The time is "; echo date('H:i:s'); // Hours, minutes, seconds echo " and the date is "; echo date('j F Y'); // Day name, month name, year ?>
Listing 1.4 includes a header comment block that contains the filename and a brief description, as well as inline comments that show what each date command will produce.