Introduction to Variables and Constants
A program contains two basic things: commands and values. The commands all have certain tasks they perform; the values are the information that the tasks are performed with. Until now, all your programs have used literal values; that is, any time a value has been specified, it has been hard coded into the program.
For example, take a look at an echo statement found in Chapter 1, “Welcome to PHP”:
echo "World!";
This statement performs the echo command (which we know inserts text) using the literal value given after it (”World!”).
NOTE
A literal is a value that you give explicitly within a program. For example, 5, 5.5, and “World!” are all literals.
The point of a program isn’t to say “World!” all the time. That could be accomplished with a regular HTML page. The whole point of writing a program is to have a uniform task performed with whatever information is specified. For example, you might want it to be able to say “Earth” instead of “World.”
Let’s consider what you’re planning to do. You know the program should output something, either way. Thus, you know you want the program to do something like this:
echo something;
You just don’t know what something is. To specify what something is, use a variable. A variable is a name that represents a value. The value of a variable may change throughout a program, or it may stay the same once it’s set.
For example, you might use a variable called $strText to insert whatever text is being used for a particular execution of the program.
NOTE
The name $strText is preceded by a dollar sign because that’s how PHP recognizes it as a variable.
Also, if you’re curious, the str prefix is an abbreviation for “string” (the type of data will be character string); it’s purpose is a matter of style, only.
Don’t worry about it right now; we’ll discuss data types and style conventions related to variables later in this chapter.
The following program uses the variable $strText, instead of a literal:
TIP
For now, don't be too distracted by the line that says:
$strText = $HTTP_GET_VARS['strText'];
This statement takes whatever value is specified in the URL and stores it in $strText; since it’s more related to program input, we won’t discuss it further until Chapter 3, “Program Input and Output (I/O).”
Try running this program with the following URLs (replacing www.example.com/path/ with the appropriate address for this program):
http://www.example.com/path/ch02ex01.php?strText=World
http://www.example.com/path/ch02ex01.php?strText=Earth
http://www.example.com/path/ch02ex01.php?strText=PHP%20Programmer
The output changes each time you run the program, depending on what URL you specify. Although this program is very basic, it shows the definite usefulness of variables.
TIP
The URLs shown aren’t as complex as they might seem; we’ll pick these apart and discuss them in Chapter 3. For now, notice that in each URL, a different value is specified for strText, and when you enter each URL, the program displays the corresponding value.
It is important to note that although variables and constants serve the same purpose, they aren’t at all identical.
Whereas, the value of a variable might change any number of times throughout the execution of a program, constants are values that will never change (and in fact can’t change) during the execution of the program.
Constants are often used to set program options. For instance, a constant named DEBUG might be used to determine how much information should be given to the visitor if something goes wrong—either a simple error message to tell the visitor there’s a problem or a detailed error message to help the programmer resolve problems within the script.