Array:


 An array stores multiple values in one single variable.

What is an Array?


A variable is a storage area holding a number or text. The problem is, a variable will hold only one value. An array is a special variable, which can store multiple values in one single variable.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:


Text Box: $cars1="Saab";
$cars2="Volvo";
$cars3="BMW";


However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The best solution here is to use an array!

An array can hold all your variable values under a single name. And you can access the values by referring to the array name.
Each element in the array has its own index so that it can be easily accessed.


There are two methods to create an array.

1.     In the following example the index are automatically assigned (the index starts at 0):


Text Box: $cars=array("Saab","Volvo","BMW","Toyota");


2.     In the following example we assign the index manually:


Text Box: $cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";

Example

In the following example you access the variable values by referring to the array name and index:


<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
The code above will output:


Text Box: Saab and Volvo are Swedish cars.

No comments:

Post a Comment

=>Advanced Java Programming (J2EE)

1.  Syllabus 2. Unit Wise Question/Material 3. Paper 4. Previous Paper