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..
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
Thus if we use , 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
case you want to have the third value of the array named $myArray
stored in a variable named $myVariable we could use
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)
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 ....
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.
= array (element1, element2, ..., elementN);
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.
is a second way to declare an array variable. It is possible
to assign a value to each position of the array through the
$varArray  = element2;
$varArray [N-1] = elementN;
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.
: 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 to Array.
Adding elements to an array
elements to an array (append instruction) can be possible in
PHP using the following syntax
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 ..... arrayname) and
you had the above statement, the new value would be assigned
to 6th element of that array (arrayname).
of using Arrays
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.
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
= array ("<BR>","Welcome to ",
"Bit After Bit. ", "Loading, just ",
5, " minutes");
" or a little more!";
script above would generate an HTML page which displays the
to Bit After Bit. Loading, just 5 minutes
Welcome to Bit After Bit. Loading, just 5 minutes or a
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".
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.
= array (
"key1" => element1;
"key2" => element2;
"keyN" => elementN;
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
"name" => "Bit After Bit",
"URL" => "http://www.bitafterbit.com",
"email" => "firstname.lastname@example.org",
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
Once we have defined such an array we could use the following
script to display some of the values
echo ("Surf to the website $website[name] at $website[URL]!");
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.