Arrays

Brackets, brackets...

You must have begun to notice that brackets have to come in pairs, and there are a lot of them. Arrays are indexed, and the index goes in [square brackets]. Here is a brief rundown of what the different backets are used for:
<tag>		- HTML tag
"quotes" - Ah, same quote for begin and end
{curley} - grouping of CSS styles, or program statements
(round) - conditions for if and loops, function arguments
/* comments */ - in CSS, C and PHP

[square] - holds index for array

Arrays hold a collection of values

A normal variable holds a single value.  However, a variable may also be an array, which can hold several values, for instance scores on a golf card (one value for each hole). Traditionally, if there are 9 values, they will be indexed by 0, 1, 2, ... 8, -- much like a building, where you must go up one level to get to the "first" floor.Individual elements of an array are specified by giving an index, in square brackets. If you achieve a rare "hole in one" on the 9th hole, set:
$score[8] = 1;

Arrays in PHP

PHP takes a very generous view of arrays, in that an element's value can itself be an array (an array within an array), and the index, or "key" can be a string, rather than an integer.
$score = array(5,5,5,5,5,5,5,5,100);	// scores for 9 holes
$score[8] = 1; // lucky on the 9th
foreach ($score as $strokes) // get score for each hole
$total = $total + $strokes; // add them up
print "I shot $total today\n"; // should print 41

Key - Value pairs

Often it is mor natural to identify values by names (strings) rather than integers. For instance, days of the week could be identified by 'sun', 'mon', ... 'sat' rather than 0,1,2,...6. PHP allows strings as array indexes. [For the technically minded, PHP arrays are implemented as an "ordered map"]

$topics = array ('mon' => 'HTML', 'wed' =>'CSS', 'fri' => 'PHP');
print topics['fri']; // will print: PHP
print_r ($topics); /* will print the array as follows:
Array
(
[mon] => HTML
[wed] => CSS
[fri] => PHP
) */

In particular, the names and values from a form are put into an array named $_REQUEST. The value for <input name="first"> will be found in $_REQUEST['first']. In the example below is a query string, and PHP statements that would create the array, but note that PHP does this automatically for you, and a variant of foreach that gets both the key and the value.

/*  Query string: first=Charlie&last=Brown&position=Pitcher
$_REQUEST['first'] = "Charlie";
$_REQUEST['last'] = 'Brown';
$_REQUEST['position'] = "Pitcher"; -- stored as if we had done */
foreach ($_REQUEST as $key => $value)
print " $key is $value<br>\n"; /* this prints:
first is Charlie
last is Brown
position is Pitcher */

The traditional for loop

If we want to, say, count up to some limit, there are 3 essential steps:
(1) initialize our counter; (2) test for limit; (3) increment the counter before repeating. This can be done in a while loop. However, the for loop groups these control statements together, lest you forget one. The following loops are equivalent. Note: only use this with an array if it uses default integer keys.

$x = 0;
while ($x < 9) for ($x = 0; $x < 9; $x++)
{ $total = $total + $score[$x]; $total += $score[$x];
$x++; } // add 1

Lin Jensen,