Visit the Official PHP Website
By James N Hitz
Creating List Arrays
To create an array we make use of the array() function thus:
$arrayName = array();
In this case $arrayName is a name I have decided to give my array. It is a variable name (only it has several rooms) and must therefore adhere to the rules for creating variables which are:
The code therefore simply creates an empty array called $arrayName.
It is also possible to create an array and fill it with values straight array. Supposing we wanted to create an array to hold names of different fruits, we would code:
$fruits = array("oranges","mangoes","pawpaws");
...this creates a new array called $fruits that now contains values and looks something like this:
To print any of the values, you simply refer to it's 'room number' (counting from zero) eg.
print "$fruits\n"; print $fruits;
Refering to our array created above, this will print:
This is because the first item in our array is "oranges" (room 0), followed by mangoes(room 1)... So printing $fruits prints the first item from the array while $fruits prints "pawpaws" - the item in room 2, the third room. Smart eh?!
Take note of the \n. This as you will remember is a newline character causing the carriage return (also called Enter) to be printed. Of course your browser is very good at ignoring newline characters - that is why we use the P tag and not the newline.
What if you wanted to add an item to an array?