An Introduction to PHP | 3 | WebReference

An Introduction to PHP | 3

An Introduction to PHP, Pt. 2

This is the conclusion of An Introduction to PHP, an excerpt from the book: Core PHP Programming 3/E by Prentice Hall.

Listing 1.7 Reformatting for readability

<html>
<head>
<title> Listing 1-7</title>
</head>
<body>
Today's date:
<?php
/*
** print today's date
*/
print( Date(" l f d, y"));
?>
</body>
</html>

You may also notice the line of code in Listing 1.7 that begins with a slash followed by an asterisk. This is a comment. Everything between /* and */ is equivalent to whitespace. It is ignored. Comments can be used to document how your code works. Even if you maintain your own code, you will find comments necessary for all but simple scripts.

In addition to the opening and closing comment statements, PHP provides two ways to build a single-line comment. Double slashes or a pound sign will cause everything after them to the end of the line to be ignored by the parser.

After skipping over the whitespace and the comment in Listing 1. 7, the PHP parser encounters the first word: print. This is one of PHP's functions. A function collects code into a unit you may invoke with its name. The print function sends textto the browser. The contents of the parentheses will be evaluated, and if it produces output, print will pass it along to the browser.

Where does the line end? Unlike BASIC and JavaScript, which use a line break to denote the end of a line, PHP uses a semicolon. On this issue PHP takes inspiration from C.

The contents of the line between print and ; is a call to a function named date. The text between the opening and closing parentheses is the parameter passed to date. The parameter tells date in what form you want the date to appear. In this case we've used the codes for the weekday name, the full month name, the day of the month, and the four-digit year. The current date is formatted and passed back to the print function.

The string of characters beginning and ending with double quotes is called a string constant or string literal. PHP knows that when quotes surround characters, you intend them to be treated as text. Without the quotes, PHP will assume you are naming a function or some other part of the language itself. In other words, the first quote is telling PHP to keep hands off until it finds another quote.

Notice that print is typed completely in lowercase letters, yet date has a leading uppercase letter. I did this to illustrate that PHP takes a lenient attitude toward the names of its built-in functions. Print, PRINT, and PrInT are all valid calls to the same function. However, for the sake of readability, it is customary to write PHP's built-in functions using lowercase letters only.

1.7 Saving Data for Later

Often it is necessary to save information for later use. PHP, like most programming languages, offers the concept of variables. Variables give a name to the information you want to save and manipulate. Listing 1.8 expands on our example by using vari-ables (see Figure 1. 2).

The first block of PHP code puts values into some variables. The four variables are YourName, Today, CostOfLunch, and DaysBuyingLunch. PHP knows they are variables because they are preceded by a dollar sign ($). The first time you use a variable in a PHP script, some memory is set aside to store the information you wish to save. You don't need to tell PHP what kind of information you expect to be saved in the variable; PHP can figure this out on its own.

The script first puts a character string into the variable YourName. As I noted earlier, PHP knows it's textual data because I put quotes around it. Likewise, I put today's date into a variable named Today. In this case PHP knows to put text into the variable because the date function returns text. This type of data is referred to as a string, which is shorthand for character string. A character is a single letter, number, or any other mark you make by typing a single key on your keyboard.

Notice that there is an equal sign (=) separating the variable and the value you put into it. This is the assignment operator. Everything to its right is put into a variable named to its left.


Created: March 27, 2003
Revised: Sept 1, 2003

URL: https://webreference.com/programming/php/corephp/2