Wednesday, September 10, 2008

Data types in PHP

In PHP the data type of a variable is not set by the programmer. PHP decides the data type of variables after interpreting the web page. Data Types in PHP include:
Numeric data type
String data type
Array data type
Numeric Data types
1. Integer Data Type
Integer data types contain whole number values. Integer values range from -32767 to 32768.
2. Doubles
Double contains floating point numbers.
String Data types
A String data type is used to contain textual information or letters. The value is assigned within quotes as shown below.

Array Data Type
An array is a variable that contains several variables of the same data type.
Each element in an array can be accessed using an id or index.
Arrays can be classified into:
Numeric arrays
Associative arrays
Multi-dimensional arrays
Numeric arrays
Elements in a numeric array are stored with a numeric index.
We could either assign the index manually or let PHP to assign it.
The IDs could be used in your PHP code to access the elements in the array.

Associative Arrays
In associative arrays, each ID is associated with a value. That is, you can use the values themselves as keys and assign values to them.
There are two ways of creating an associative array as shown in the example.Multi-dimensional Arrays.
In multi-dimensional arrays, an array element can have another array as its value and these values can in turn have arrays as their values.
Multi-dimensional arrays can be two-dimensional or three-dimensional.
While, two-dimensional arrays can be perceived as having a row and a column, three-dimensional arrays have one more dimension. That is, a three dimensional array has many two-dimensional arrays.
EXAMPLES:
1.
Assigning indices manually
$student[1] = ?Maria?;
$student[2] = ?John?;
$student[3] = ?Mathew?;?

2.
Assigning IDs automatically?
$students = {?Maria?,John?,?Mathew?};?
In this case, the indices are automatically assigned by PHP.??
3.
echo $students[0];
echo ?
?;
echo $students[2];????
Let us use an associative array to store the names and salaries of a few employees:?
One way of creating an associative array:
$salary = {?James?=>2000, ?Maira?=>5000, ?Mathew?=>3000};?
You can also create an associative array like this:?
$salary[?James?] = ?2000;
$salary[?Maria?] = ?5000;
$salary[?Mathew?] = ?3000?;
You could access elements of associative arrays as:
echo ?James?s salary:?;
echo ?
?;
echo $salary[?James?];???
Let?s assume that you need to store the names, IDs and ages of a few employees in an array. You can do that by using a two-dimensional array.
$employee = array{array(?John?, 223,42),
array(?Daisy?,443,50),
array(?Mathew?,990,37)
};?
You can easily access the values of the arrays as follows:?
echo ?Name:?;
echo $employee[0][0];
echo ?
?;
echo ?ID:?;
echo $employee[0][1];
echo ?
?;
echo ?Age:?;
echo $employee[0][2];?
A multi-dimensional array can also be an array of associative arrays.
That is, the above array can also be created like this:?
$employee = array{array(Name=>?John?,???????????????????? ID=>223,Age=>42),
array(Name=>?Daisy?,???????????????????? ID=>443,Age=>50),
array(Name=>?Mathew?,???????????????????? ID=>990,Age=>37)
}?
In this case, you may access the
array values like this:?
echo ?Name:?;
echo $employee[2][?Name?];
echo ?
?;
echo ?ID:?;
echo $employee[2][?ID?];
echo ?
?;
echo ?Age:?
echo $employee[2][?Age?];
To understand three-dimensional arrays better, let us assume that we need to store the details of employees in different departments in a single array.
We could do that as follows:?
$shop = array
(
array(
array("John", 223, 42),
array("daisy", 443, 50),
array("Mathew", 990, 37)
??????? ),
array(
array("Smith", 390, 45),
array("Clive", 230, 52),
array("Stephen", 900, 32)
???????? ),
array(array("Chris", 553, 35),
array("Natasha", 725, 45),
array("Jack", 617, 47)
??????? )
);?
Each element of the above three-dimensional array is a two-dimensional array. So you could store all employees under a category in a two-dimensional array ie., in an element of the three-dimensional array.?
The array elements could be accessed as follows:
echo $employee[0][0][0];
echo ?
?;
echo $employee[0][0][1];
echo ?
?;
echo $employee[0][0][2];

No comments: