PHP provides three primitive data types: integers, floating point numbers, and strings. In addition, there are two compound data types: arrays and objects.
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 can 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 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
range from 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
Note that PHP also has a set of functions known as the BC (binary calculator) functions. These functions can manipulate arbitrary precision numbers. If you are dealing with very large numbers or numbers that require a high degree of precision, you should use these functions.
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 $a ";
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 $a ';
In this case, the output is exactly “Hello $a ”. There is no variable substitution or handling of escape sequences.
The following table shows the escape sequences understood by PHP:
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 non-consecutive
elements, PHP selects the index value that is one greater than the
current highest index value. This auto-indexing feature is most
useful when dealing with multiple-choice HTML
<SELECT>
form elements, as we’ll see
in a later example.
Although we have called strings a primitive data type, it is actually possible to treat a string as a compound data type, where each character in the string can be accessed separately. In other words, you can think of a string as an array of characters, where the first character is at index 0. Thus, you can pick the third character out of a string with:
$string[2]
Arrays can also be indexed using strings; these kinds of arrays are called associative arrays:
$var["January"]=1; $var["February"]=2;
In fact, you can use a mix of numerical and string indices with a single array. That is because internally PHP treats all arrays as hash tables and the hash, or index, can be whatever you want.
All arrays in PHP can be traversed safely with the following mechanism:
while(list($key,$value)=each($array)) { echo "array[$key]=$value<br> "; }
This is the most common way to loop through each element of an array, whether it is a linear or an associative array. PHP provides a number of array manipulation functions; these are detailed later in Section 1.13.
An object is a compound data type that can contain any number of variables and functions. PHP’s support for objects is very basic in Version 3. PHP Version 4 will improve the object-oriented capabilities of PHP. In PHP 3.0 the object-oriented support is designed to make it easy to encapsulate data structures and functions in order to package them into reusable classes. Here’s a simple example:
class test { var $str = "Hello World"; function init($str) { $this->str = $str; } } $class = new test; print $class->str; $class->init("Hello"); print $class->str;
This code creates a test
object using the new
operator. Then it sets a variable called str
within the object. In object-speak, a variable in an object is known
as a property of that object. The test
object also
defines a function, known as a method, called init( )
. This method uses the special-purpose
$this
variable to change the value of the
str
property within that object.
If you are familiar with object-oriented programming, you should recognize that PHP’s implementation is minimal. PHP3 does not support multiple inheritance, data protection (or encapsulation), and destructors. PHP does have inheritance and constructors, though.
Every value in PHP has a boolean truth value (true
or false
) associated with it. This value is
typically used in control structures, like if/else
and for
. The boolean value associated with a data
value is determined as follows:
For an integer or floating point value, the boolean value is
false
if the value is 0
;
otherwise the boolean value is true
.
For a string value, the boolean value is false
if
the string is empty; otherwise the boolean value is
true
.
For an array, the boolean value is false
if the
array has no elements; otherwise the boolean value is
true
.
For an object, the boolean value is false
if the
object has no defined variables or functions; otherwise the boolean
value is true
.
For an undefined object (a variable that has not been defined at
all), the boolean value is false
.
PHP has two built-in keywords, true
and
false
, where true
represents
the integer value 1
and false
represents the empty string.
Variables in PHP do not need to be explicitly typed. PHP sets the type when a variable is first used in a script. You can explicitly specify a type using C-style casting.
For example:
$var = (int) "123abc";
Without the (int)
in this example, PHP creates a
string variable. With the explicit cast, however, we have created an
integer variable with a value of 123. The following table shows the
available cast operators in PHP:
Operators |
Function |
---|---|
|
Cast to an integer |
|
Cast to a floating point number |
|
Cast to a string |
|
Cast to an array |
|
Cast to an object |
Although they are not usually needed, PHP does provide the following
built-in functions to check variable types in your program:
gettype( )
, is_long( )
,
is_double( )
, is_string( )
,
is_array( )
, and is_object( )
.
18.226.187.24