Prev | Current Page 202 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

(The explode() function is formally introduced in Chapter 9.) These elements
are then assigned to $name, $occupation, and $color. At that point, it??™s just a matter of
formatting for display to the browser.
Populating Arrays with a Predefined Value Range
The range() function provides an easy way to quickly create and fill an array consisting
of a range of low and high integer values. An array containing all integer values in this
range is returned. Its prototype looks like this:
array range(int low, int high [, int step])
For example, suppose you need an array consisting of all possible face values of
a die:
$die = range(0,6);
// Same as specifying $die = array(0,1,2,3,4,5,6)
But what if you want a range consisting of solely even or odd values? Or a range
consisting of values solely divisible by five? The optional step parameter offers a
convenient means for doing so. For example, if you want to create an array consisting
of all even values between 0 and 20, you could use a step value of 2:
CHAPTER 5 ?–  ARRAYS 133
$even = range(0,20,2);
// $even = array(0,2,4,6,8,10,12,14,16,18,20);
The range() function can also be used for character sequences. For example,
suppose you want to create an array consisting of the letters A through F:
$letters = range("A","F");
// $letters = array("A,","B","C","D","E","F");
PRINTING ARRAYS FOR TESTING PURPOSES
So far the array contents in the previous examples have been displayed using comments.


Pages:
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214