1.6. Using PHP Variables

Variables are containers used to hold information. A variable has a name, and information is stored in the variable. For instance, you might name a variable $age and store the number 12 in it. After information is stored in a variable, it can be used later in the script. One of the most common uses for variables is to hold the information that a user types into a form.

1.6.1. Naming a variable

When you're naming a variable, keep the following rules in mind:

  • All variable names have a dollar sign ($) in front of them. This tells PHP that it is a variable name.

  • Variable names can be any length.

  • Variable names can include letters, numbers, and underscores only.

  • Variable names must begin with a letter or an underscore. They cannot begin with a number.

  • Uppercase and lowercase letters are not the same. For example, $firstname and $Firstname are not the same variable. If you store information in $firstname, for example, you can't access that information by using the variable name $firstName.

When you name variables, use names that make it clear what information is in the variable. Using variable names like $var1, $var2, $A, or $B doesn't contribute to the clarity of the script. Although PHP doesn't care what you name the variable and won't get mixed up, people trying to follow the script will have a hard time keeping track of which variable holds what information. Variable names like $firstName, $age, and $orderTotal are much more descriptive and helpful.


1.6.2. Creating and assigning values to variables

Variables can hold numbers or strings of characters. You store information in variables with a single equal sign (=). For instance, the following four PHP statements assign information to variables:

$age = 12;
$price = 2.55;
$number = −2;
$name = "Little Bo Peep";

Notice that the character string is enclosed in quotes, but the numbers are not. We discuss more about using numbers and characters in the section "Understanding Data Types," later in this chapter.

Whenever you put information into a variable that didn't exist before, you create that variable. For instance, suppose you use the following PHP statement:

$firstname = "George";

If this statement is the first time that you've mentioned the variable $firstname, this statement creates the variable and sets it to "George". If you have a previous statement setting $firstname to "Mary", this statement changes the value of $firstname to "George".

You can also remove information from a variable. For example, the following statement takes information out of the variable $age:

$age = "";

The variable $age exists but doesn't contain a value. It doesn't mean that $age is set to 0 (zero) because 0 is a value. It means that $age doesn't store any information. It contains a string of length 0.

You can go even further and uncreate the variable by using this statement:

unset($age);

After this statement is executed, the variable $age no longer exists.

1.6.3. Using variable variables

PHP allows you to use dynamic variable names, called variable variables. You can name a variable with the value stored in another variable. That is, one variable contains the name of another variable. For example, suppose you want to construct a variable named $city with the value Los Angeles. You can use the following statement:

$name_of_the_variable = "city";

This statement creates a variable that contains the name that you want to give to a variable. Then, you use the following statement:

$$name_of_the_variable - "Los Angeles";

Note the extra dollar sign ($) character at the beginning of the variable name. This indicates a variable variable. This statement creates a new variable with the name that is the value in $name_of_the_variable, resulting in the following:

$city = "Los Angeles";

The value of $name_of_the_variable does not change.

The following example shows how this feature works. In its present form, the script statements may not seem that useful; you may see better way to program this task. The true value of variable variables becomes clear when they are used with arrays and loops, as discussed in Chapter 2 of this minibook.

Suppose you want to name a series of variables with the names of cities that have values that are the populations of the cities. You can use this code:

$Reno = 360000;
$Pasadena = 138000;
$cityname = "Reno";
echo "The size of $cityname is ${$cityname}";
$cityname = "Pasadena";
echo "The size of $cityname is ${$cityname}";

The output from this code is:

The size of Reno is 360000
The size of Pasadena is 138000

Notice that you need to use curly braces around the variable name in the echo statement so that PHP knows where the variable name is. If you use the statement without the curly braces, the output is as follows:

The size of Reno is $Reno

Without the curly braces in $$cityname, PHP converts $cityname to its value and puts the extra $ in front of it, as part of the preceding string.

1.6.4. Displaying variable values

You can display the value in a variable by using any of the following statements:

  • echo

  • print_r

  • var_dump

1.6.4.1. Using variables in echo statements

You can display the value in a variable on a Web page with an echo statement. For instance, if you use the following PHP statement in a PHP section:

echo $age;

the output is 12. If you include the following line in an HTML file:

<p>Your age is <?php echo $age ?>.</p>

the output on the Web page is

Your age is 12.

Table 1-3 shows the use of variables in some echo statements and their output. For the purposes of the table, assume that $string1 is set to Hello and $string2 is set to World!.

Table 1.3. echo Statements
echo StatementOutput
echo $string1;Hello
echo $string1,$string2;HelloWorld!
echo "$string1 $string2";Hello World!
echo "Hello ",$string2;Hello World!
echo "Hello"," ",$string2;Hello World!
echo '$string1',"$string2";$string1World!

Double quotes and single quotes have different effects on variables. When you use single quotes, variable names are echoed as is. When you use double quotes, variable names are replaced by the variable values.


Sometimes you need to enclose variable names in curly braces ({ }) to define the variable name. For instance, the following statements

$pet = "bird";
echo "The $petcage has arrived.";

won't output bird as the $pet variable. In other words, the output won't be The birdcage has arrived. Rather, PHP will look for the variable $petcage and won't be able to find it. You can echo the correct output by using curly braces to separate the $pet variable:

$pet = "bird";
echo "The {$pet}cage has arrived.";

The preceding statement gives you

The birdcage has arrived.

A variable keeps its information for the entire script, not just for a single PHP section. If a variable is set to "yes" at the beginning of a file, it will still hold "yes" at the end of the page. For instance, suppose your file has the following statements:

<p>Hello World!</p>
<?php
    $age = 15;
    $name = "Harry";
?>
<p>Hello World again!</p>
<?php
    echo $name;
?>

The echo statement in the second PHP section will display Harry. The Web page resulting from these statements is

Hello World!

Hello World again!

Harry

1.6.4.2. Displaying variables with print_r statements

PHP provides a function named print_r for looking at the value in a variable. You can write the following statements to display a variable value:

$weekday = "Monday";
print_r($weekday);

The output from print_r is:

Monday

1.6.4.3. Displaying variables with var_dump statements

PHP provides a function named var_dump that you can use to display a variable value and its data type. (Data types are discussed in detail in the section "Understanding Data Types," later in this chapter.)

You can write the following statements to display a variable value:

$weekday = "Monday";
var_dump($weekday);

The output of var_dump is:

string(6) "Monday"

The output shows that the value in $weekday is Monday. The output also shows that the value is a string data type that is 6 characters long.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.136.186