Working with Numeric Variables

Working with Numeric Variables

Computers ultimately store information in on/off impulses. These very simple data values can be converted into a number of more convenient kinds of information. The PHP language makes most of this invisible to you, but it's still important to know that string (text) is handled differently in memory than numeric values, and there are two main types of numeric values.

Making the ThreePlusFive Program

As an example of how PHP works with numbers, consider the ThreePlusFive.php program illustrated in Figure 2.6.

Click To expand
Figure 2.6: This program does basic math on variables containing the values 3 and 5.

All the work in the ThreePlusFive program is done with two variables called $x and $y. (I know, I recommended that you assign variables longer, descriptive names, but these variables are commonly used in arithmetic problems, so these very short variable names are OK in this instance.) The code for the program looks like this:

<title>Three Plus Five</title>
<h1>Three Plus Five</h1>
<h3>Demonstrates use of numeric variables</h3>

$x = 3;
$y = 5;

print "$x + $y = ";
print $x + $y;
print "<br><br>";

print "$x - $y = ";
print $x - $y;
print "<br><br>";

print "$x * $y = ";
print $x * $y;
print "<br><br>";

print "$x / $y = ";
print $x / $y;
print "<br><br>";



Assigning Numeric Values

You create a numeric variable like any other variable in PHP. Simply assign a value to a variable, and the variable is created. Notice that numeric values do not require quotes. I created variables called $x and $y and assigned appropriate values to these variables.

Using Mathematical Operators

For each calculation, I wanted to print the problem as well as its solution. The line that says

print "$x + $y = ";

prints out the values of the $x and $y variables with the plus sign between them. In this particular case (since $x is 3 and $y is 5), it prints out the literal value

3 + 5 =

Because the plus sign and the equals sign are inside quotes, they are treated as ordinary text elements and PHP doesn't do any calculation (such as addition or assignment) with them.

The next line

print $x + $y;

does not contain any quotes. It calculates the value of $x + $y and prints the result of this calculation (8) to the Web page.

Most of the math symbols you are familiar with also work with numeric variables. The plus sign (+) is used for addition, the minus sign (-) indicates subtraction, the asterisk (*) is used for multiplication, and the forward slash (/) is used for division. The remainder of the program illustrates how PHP does subtraction, multiplication, and division.

Start example

Those numbers without any decimal point are called integers and the numbers with decimal values (like 1.5, 0.333, and so on) are called real numbers. These two types of numbers are stored differently in computers, and this distinction sometimes leads to problems. PHP does its best to shield you from this type of issue. For example, since the values 3 and 5 are both integers, the results of the addition, subtraction, and multiplication problems are also guaranteed to be integers. However, the quotient of two integers is often a real number. Many languages would either refuse to solve this problem or would not give a complete result. They might say that 3 / 5 = 0 rather than 0.6. PHP tries to convert things to the appropriate type whenever possible, and it usually does a pretty good job. There are times, however, that you will need to control this behavior. The setType() function lets you force a particular variable into a particular type. You can look up the details in the online help for PHP (included in the CD-ROM that accompanies this book).

End example