Your First Script
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
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.
The time is
<?php echo date('H:i:s');?>
and the date is
<?php echo date('j F Y');?>
![]() | Web Document Location If you were using a default Apache installation in Windows, you would create time.php in the folder C:\Program Files\Apache Group\Apache\htdocs, and the correct URL would be http://localhost/time.php. |
If you insert a space character after ?>, that line now contains non-PHP elements, and the output is spaced correctly.
The time is
15:33:09and the date is
13 October 2004
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
The non-dynamic text elements you want to output are contained in quotation marks. Either double quotes (as used in Lesson 2, "Variables." The following statements are equally valid:
<?php
echo "The time is ";
echo date('H:i:s');
echo " and the date is ";
echo date('j F Y');
?>
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:
echo "The time is ";
echo 'The time is ';
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.
The time is 15:59:50 and the date is 13 October 2004
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'
);
?>
![]() | Using Newlines If you wanted to send an explicit newline character to the web browser, you could use the character sequence \n. There are several character sequences like this that have special meanings, and you will see more of them in Lesson 6, "Working with Strings." |
Comments
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.
Comment | Description |
---|---|
// or # | Single-line comment. Everything to the end of the current line is ignored. |
/* ... */ | Single- or multiple-line comment. Everything between /* and */ is ignored. |
Listing 1.4. Using Comments in a Script
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.
<?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
?>