PHP—Variables

Variables are placeholders for data. You may recall numeric variables from math, but variables can represent a wide variety of data types, for instance, a last name, a date, a type of car, a program name, or a message.

In most programming languages, variables are typed, that is, they are categorized by the particular type of data they refer to. In PHP, variables have the following types:

    1. integers (numbers without a decimal point, e.g., 0, 23, -234)

    2. floats (numbers with a decimal point, e.g., 3.1415926, -11.5)

    3. strings (text items, e.g., "Howdy, partner!")

    4. arrays (a list of integers, floats, or strings, e.g., {1, 2, 3, 4, 5})

Integers and floats are different types because they are stored differently in the computer. In particular, an integer is stored in a straightforward way as a binary number. This means that the size of the integer is limited by the size of the storage unit. If an integer is stored in 32 bits, then no more than 4,294,967,296 different integers can be represented: numbers from 0 through 4,294,967,295 or else numbers from -2,147,483,648 to 2,147,483,647. Moreover, fractional numbers cannot be stored (you cannot store, for example, 1/2 in a single integer variable).

By contrast, floats can store much larger and smaller values because floats are stored in scientific notation as a mantissa and an exponent. Thus, a float is represented as one number (the mantissa) multiplied by 10 raised to a power (the exponent). An example of such a number would be 1.234 x 10^-3 (= 0.001234). The drawback to floats is that, because the storage space has to store two numbers, instead of one for an integer, the precision of a number is smaller than for an integer. For instance, using 32 bits you could express 4,000,000,001 as an integer, but with a float you would lose the final 1. In general, you should not use floats when you need to keep track of a precise value.

Strings are text items. Strings can contain numbers, characters, symbols, and even variables. Strings are indicated by putting a bunch of text in quotation marks. If you want a string to contain a quotation mark, you have to specially indicate it with what is called an escape character, a backslash (\). For instance, suppose you want to represent the following:

Harry said, "I like it!"

You would represent it like this:

"Harry said, \"I like it!\""

Thus, a PHP script outputting the above would look like this:

<html>
<head>
<title>
PHP Example 2
</title>
</head>
<body>
<center>
Here we go . . .
</center><br><Br>
<?php

echo "Harry said, \"I like it!\"";

?>
</body>
</html>


In PHP, all variables are preceded by a dollar sign ($). Accordingly, here are some examples of different types of variables and their representation.

<html>
<head>
<title>
PHP Example 3
</title>
</head>
<body>
<center>
Mortgage Rates
</center><Br><Br>
<?php

$years = 30;
$interest_rate = 6.25;
$bank = "Heritage Cooperative Bank";

echo "$bank offers a $years-year mortgage at $interest_rate percent."

?>
</body>
</html>

Notice that you can include variables in quotes. Here's the output the above script produces:

Mortgage Rates

Heritage Cooperative Bank offers a 30-year mortgage at 6.25 percent.

Here are some rules regarding variable names:

    1. Variable names must begin with a dollar sign ($).

    2. Variable names must not have any spaces (e.g., $first name is no good; use $first_name instead).

    3. Variable names must not begin with a number (e.g., $1stName is no good).

    4. Variable names are case-sensitive ($bank is different from $Bank which is different from $BANK).

Arrays are lists of integers, floats, or strings. An element of an array is referenced by a subscript, which is an integer (or integer variable) enclosed in brackets ([]). For example, you might have an array of names called $names. To reference the third name, you would type $names[3]. If you had an integer variable (call it $i) that was set to some value (say, 7), and you wanted to reference the $ith name, you would type $names[$i]. here is an example:

<?php

$names[1] = "Steve";
$names[2] = "Charlotte";
$names[3] = "Seamus";
$names[4] = "Toby";
$names[5] = "Rachael";

$i = 3;
$j = 4;

echo "Name $i = $names[$i]<br>";
echo "Name $j = $names[$j]<br>";

?>


That PHP code produces the following result. (Note that <br> is an HTML tag that inserts a new line.)

Name 3 = Seamus
Name 2 = Toby


Next (Retrieving System Information)