Creating and Outputting Variables
Let's look at how variables are created and used in PHP. PHP is a weakly typed language, meaning that you don't have to declare a variable to be a particular type before using it. Instead, you can simply create it by giving it a name preceded by a dollar sign ($). For example, we'll eventually be able to determine dynamically how many days are in the month we're displaying, but for now we'll set an arbitrary value, as shown in Listing 3.
Listing 3Creating a Variable
<html> <head><title>Event Calendar</title></head> <body> <h1>This Month's Events</h1> <?php $numberofdays = 31; echo ("The month in question has $numberofdays days in it."); ?> </body> </html>
Notice the echo() statement here. Rather than concatenating the strings of text with the variable, we've simply inserted the variable directly into the string. When PHP processes the page, it replaces the variable with the appropriate value, as shown in Figure 2.
Figure 2 Displaying the value of a variable.
Of course, this capability could easily become a liability in a situation where you really wanted to output a string that includes a variable name. Fortunately, simply "escaping" the dollar sign with a backslash solves the problem, so the function
echo ("The variable name is \$numberofdays.");
The variable name is $numberofdays.
The variable name is 31.
You can also explicitly concatenate strings and variables using a period (.) as the concatenation character, like this:
echo ("The month in question has ".$numberofdays." days in it.");
PHP recognizes eight different types of variables. The simplest are the primitive types, boolean, integer, float, and string, which behave just as they do in other languages. The other four consist of NULL, which is a variable with no value; resource, which we'll see in Part 2 of this series; object, which we'll see in Part 3; and array.
An array variable can hold multiple values, referencing them by a key value. That key can be an integer or a string value. For example, Listing 4 creates a simple array and then outputs one of the values:
Listing 4Using a Simple Array
<html> <head><title>Event Calendar</title></head> <body> <h1>This Month's Events</h1> <?php $numberofdays = 31; $daysoftheweek = array("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"); echo("Day number three is ".$daysoftheweek."."); ?> </body> </html>
In PHP, an array is zero-based, meaning that the first value has an index of 0. For this reason, day number 3 is actually the fourth item, as shown in Figure 3.
Figure 3 Using an array.
In some cases, you want to create an array that uses strings as the index, so you can treat the array as a series of name-value pairs. You can do this by specifying the key when you create the array, as shown here:
$daysoftheweek = array("S"=>"SUNDAY", "M"=>"MONDAY", "T"=>"TUESDAY", "W"=>"WEDNESDAY", "R"=>"THURSDAY", "F"=>"FRIDAY", "SA"=>"SATURDAY"); echo("Day 'R' is ".$daysoftheweek["R"].".");
In each pair, such as "S"=>"SUNDAY", the first value is the key, and the second is the value.
Now that we've got some variables, let's do something with them.