Search This Blog

Friday, 8 February 2013

Compound and Special Data Types in PHP

0 comments

Handling Arrays and Objects in PHP

An array holds a group of values, which we can identify by position (a number, with zero being the first position) or some identifying name (a string), called an associative. Array can be created in two ways:
In the first method, an array is created by just assigning values to it using the index or associative:
$Person[0] = “Edison”;
$Person[1] = “Wankel”;
$Person[2] = “Crapper”;
These lines of code create an array named Person with three elements numbered 0, 1 and 2. Now, consider the following lines of code:
$Creator[‘Light Bulb’] = “Edison”;
$Creator[‘Rotary Engine’] = “Wankel”;
$Creator[‘Toilet’] = “Crapper”;
The above set of codes will create an array named Creator with three elements, each of them are identified with a name (called associative).
In the second method, we create the array using the array() construct as follows:
$Person = array(‘Edison’, ‘Wankel’, ‘Crapper’);
$Creator = array(‘Light Bulb’ => ‘Edison’, ‘Rotary Engine’ => ‘Wankel’, ‘Toilet’ => ‘Crapper’);
Once an array is created, we can loop through the elements of the array in several ways. The most common way of doing this is using a foreach loop:
foreach ($person as $name)
{
echo “Hello, $name \n”;
}
Here, the variable $name is used as loop variable, which will hold the ith (with i starts from 0, and incremented after each iteration) element of array during each iteration. A similar forloop for looping through an array, which identifies its elements using associative (name) is as follows:
foreach ($creator as $invention => $inventor)
{
echo “$inventor created the $invention\n”;
}
In this foreach loop, we use two loop variables, one ($invention) for holding the associative and the other one ($inventor) for storing the value stored in the ith element of the array.
We can sort the elements of an array with the sort methods – sort and assort():
sort($person);
assort($creator);
Use the function is_array() to test whether a value is an array:
if (is_array($x))
{
echo “variable x is a an Array”;
}
Objects:
Objects are runtime entities, which are created using a class and the keyword new. A class is a definition of a structure that contains properties (variables) and methods (functions). Classes are defined with the class keyword:
class Person{
public $name = “”;
function name($newName = NULL)
{
if(!is_null($newName)
{
$this name = $newName;
}
Return $thisname
}}
Once a class is defined, any number of objects can be created using it, and the objects’ properties and methods are accessed with the construct:
$ed = new Person
$edname(‘Edison’);
$tc = new Person;
$tcname(‘Crapper’);
printf(“Hello, $edname \n”);
printf(“Look out below $tcname \n”);
Use the function is_object() to know whether a variable is of type object:
if (is_object($ed)){
echo ‘$ed is an object’;
}
NULL:
The NULL value represents a variable that has no value. For e.g.,
$alpha = “beta”;
$alpha = null;
Use the is_null() function to test whether a value is NULL. This function is used generally to see whether a variable has a value or not.

Leave a Reply