Luigi
Arlotta concludes his series 'A PHP Tutorial for Beginners'
with a discussion on Arrays. He also discusses topics
such as associative arrays. His series has turned out
to be an excellent introduction to PHP programming. Especially
beginners,who are not even well versed with any kind of programming
can begin directly with his tutorial..
Arrays
An
Array is a sort of multi-valued variable. It stores one or more
values, each one identified by a number representing its position
in the array. The exact syntax is shown below
We're referring to the sixth element of the array named myArray.
Sixth because array elements always begin with
0.
Thus if we use [10], we would be referring to the 11th element
of the array.
Similarly to assign the value 5 to the second element of the
same array we could use the following
In
case you want to have the third value of the array named $myArray
stored in a variable named $myVariable we could use
| $myVariable
= $myArray[2]; |
Arrays are often used to hold values of related variables. For
example there are 100 students and all their ages have to be
stored in a program. Instead of using 100 Integer variables,
we would use one array (aptly named $ages[100])
which is an array of 100 Integers. This way it would lead to
neater and better design on the program. You could then access
the 100 elements of such an array using $ages[0] ....
$ages[99] .
Array
Declaration Methods
The
declaration of an array variable can be made by listing the
elements that the array would hold right at the time when you
declare the array.
| $varArray
= array (element1, element2, ..., elementN); |
$varArray
is the name we chose for the array variable, while element1,
element2 ... elementN are the values stored in
the array in position 1, 2... N.
There
is a second way to declare an array variable. It is possible
to assign a value to each position of the array through the
following syntax
|
$varArray[0]
= element1;
$varArray [1] = element2;
.
.
.
$varArray [N-1] = elementN;
|
Both
the above methods perform the same assignment. The first one
is more compact, but you need to know the values that you would
enter in the array right at the time when you initialize the
array. Whereas the second method involves assigning each element
of the array one at a time. So you could (if required) compute
a particular value in the middle of your program and then assign
it any particular array element.
Note
: Remember that the first element of an Array is always
identified by number zero (0). This means that if an array holds
eight elements, they will be numbered from Array[0] to Array[7].
Adding elements to an array
Adding
elements to an array (append instruction) can be possible in
PHP using the following syntax
This
instruction would append the value ' newvalue ' to a
new element after the existing last element in the array. Suppose
the array had 5 values (arrayname[0] ..... arrayname[4]) and
you had the above statement, the new value would be assigned
to 6th element of that array (arrayname[5]).
Example
of using Arrays
Using
an Array data type could be useful during data processing operations,
too. If we decide to save variables in one array object
it would be easier to manage them. This because it is possible
to process (read/write/edit) all of them by using a loop structure
(for loop or while loop). In fact the loop's
control variable would be used as the array's index.
Now
we are going to define an array and print its content with a
for loop. Then we'll add a new value to the array and
finally we'll print the array again. This is the code for the
program
|
<HTML>
<BODY>
<?
$vv
= array ("<BR>","Welcome to ",
"Bit After Bit. ", "Loading, just ",
5, " minutes");
for
($k=0;$k<6;$k++)
echo ($vv[$k]);
$vv[]=
" or a little more!";
for
($k=0;$k<7;$k++)
echo ("$vv[$k]");
?>
</BODY>
</HTML>
|
The
script above would generate an HTML page which displays the
following
|
Welcome
to Bit After Bit. Loading, just 5 minutes
Welcome to Bit After Bit. Loading, just 5 minutes or a
little more!
|
Scalar
and Associative Arrays
The array variables we've discussed till now are all scalar
arrays. Working with scalar arrays, you can access to any
elements through an index.
PHP allows to define a second kind of arrays called the associative
arrays. An associative array is an array in which the elements
can be accessed through a keyword instead of the index. Each
element, in an associative array, is characterized by a keyword.
This means we'll no more use indexes to read/write/edit elements,
because they have been replaced by keywords. The associative
arrays is constituted by pairs such as "key - element".
The
syntax we'll use to define an associative array is similar to
that we have used to define scalar array. We just need to replace
indexes with keywords.
$varArray
= array (
"key1" => element1;
"key2" => element2;
...
"keyN" => elementN;
); |
varArray
is the name of the array variable , key1, key2..., keyN are
the N keywords through which we can access the respective elements
- element1, element2, ... elementN.
Look at the following example where associative arrays are used
$website
= array(
"name" => "Bit After Bit",
"URL" => "http://www.bitafterbit.com",
"email" => "webmaster@bitafterbit.com",
); |
An
associative array with three elements has been defined. The
first element, accessible through the keyword "name"
is of string type and contains the value "Bit After Bit".
The second element is a string too, it is accessible through
keyword "URL" and stores the value "http://www.bitafterbit.com".
The third and last element is also a string containing the value
"webmaster@bitafterbit.com".
Once we have defined such an array we could use the following
script to display some of the values
Sample
Program
<?
echo ("Surf to the website $website[name] at $website[URL]!");
?>
|
Output
of above program
Surf to the website Bit After Bit at http://www.bitafterbit.com
|
That's it!! We have covered all the necessary basics that any
beginner would like to learn. I hope you'll have enjoyed this
series as much as I have enjoyed writing it..

|
©
Copyright by Luigi Arlotta. All rights reserved. Contact
the author for permissions.
|