Data Types
PHP provides four primitive data types: integers, floating
point numbers, strings, and booleans. In addition, there are two compound data
types: arrays and objects.
Integers
Integers are whole numbers. The range of integers in PHP is
equivalent to the range of the long data type in C. On 32-bit
platforms, integer values range from -2,147,483,648 to +2,147,483,647. PHP
automatically converts larger values to floating point numbers if you happen to
overflow the range. An integer can be expressed in decimal (base-10),
hexadecimal (base-16), or octal (base-8). For example:
$decimal=16; $hex=0x10; $octal=020;
Floating Point Numbers
Floating point numbers represent decimal values. The range of
floating point numbers in PHP is equivalent to the range of the double
type in C. On most platforms, a double can be between 1.7E-308 to 1.7E+308. A
double may be expressed either as a regular number with a decimal point or in
scientific notation. For example:
$var=0.017; $var=17.0E-3
PHP also has two sets of functions that let you manipulate
numbers with arbitrary precision. These two sets are known as the BC and the GMP
functions. See http://www.php.net/bc and http://www.php.net/gmp for more
information.
Strings
A string is a sequence of characters. A string can be delimited
by single quotes or double quotes:
'PHP is cool' "Hello, World!"
Double-quoted strings are subject to variable substitution and
escape sequence handling, while single quotes are not. For example:
$a="World"; echo "Hello\t$a\n";
This displays "Hello" followed by a tab and then "World"
followed by a newline. In other words, variable substitution is performed on the
variable $a and the escape sequences are converted to their
corresponding characters. Contrast that with:
echo 'Hello\t$a\n';
In this case, the output is exactly "Hello\t$a\n". There is no
variable substitution or handling of escape sequences.
Another way to assign a string is to use what is known as
Arrays
An array is a compound data type that can contain multiple data
values, indexed either numerically or with strings. For example, an array of
strings can be written like this:
$var[0]="Hello"; $var[1]="World";
Note that when you assign array elements like this, you do not
have to use consecutive numbers to index the elements.
As a shortcut, PHP allows you to add an element onto the end of
an array without specifying an index. For example:
$var[ ] ="Test";
PHP picks the next logical numerical index. In this case, the
"Test" element is given the index 2 in our $var array: if the
array has nonconsecutive elements, PHP selects the index value that is one
greater than the current highest index value. This autoindexing feature is most
useful when dealing with multiple-choice HTML <select> form
elements, as we'll see in a later example.