PHP for the World Wide Web (Visual QuickStart Guide)
I l @ ve RuBoard |
The formal method of creating an array is to use array(), the syntax of which is: $List = array ("apples", "bananas", "oranges"); In this examplewhere you have not specified an index for the elementsthe first item, apples, will automatically be indexed at 0, the second at 1, and the third at 2. You can assign the index when using array() as follows : $List = array (1=>"apples", 2=>"bananas", 3=>"oranges"); The index value you specify does not have to be a number, you could use words as well. This technique of indexing is very practical for making more meaningful lists. As an example, you could create an array which records the soup of the day for each day of the week. Script 7.1. This is another example where adding white space to the page in order to clarify your programming (when initializing the array) won't have any adverse affects and is therefore recommended.
To create an array:
Tip The practice of beginning any index at zero is standard in PHP and most other programming languages. As unnatural as it may seem, it's here to stay, so you have two possible coping techniques. First, manually start all of your arrays indexed at the position 1. Second, unlearn a lifetime of counting from one. You can decide which is easier but most programmers just get used to this odd construct.
|
I l @ ve RuBoard |