As you??™ll learn throughout
this chapter, PHP offers many ways to do so. Regardless of whether you??™re using associative
or numerical keys, keep in mind that all rely on the use of a central feature
known as an array pointer. The array pointer acts like a bookmark, telling you the
position of the array that you??™re presently examining. You won??™t work with the array
pointer directly, but instead will traverse the array using either built-in language
features or functions. Still, it??™s useful to understand this basic concept.
Creating an Array
Unlike other languages, PHP doesn??™t require that you assign a size to an array at
creation time. In fact, because it??™s a loosely typed language, PHP doesn??™t even require
that you declare the array before using it, although you??™re free to do so. Each approach
is introduced in this section, beginning with the informal variety.
Individual elements of a PHP array are referenced by denoting the element between a
pair of square brackets. Because there is no size limitation on the array, you can create
the array simply by making reference to it, like this:
$state[0] = "Delaware";
130 CHAPTER 5 ?– ARRAYS
You can then display the first element of the array $state like this:
echo $state[0];
Additional values can be added by mapping each new value to an array index,
like this:
$state[1] = "Pennsylvania";
$state[2] = "New Jersey";
.
Pages:
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211