<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
$score[8] = 1;
$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
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 */
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