3
Simply arrays store values in a numerical index.
<?
$array=array("Monday","Tuesday","Wendsday","Thursday",
"Friday","Saturday","Sunday");
For ($a=0; $a > count($array); $a++) {
echo "$array[$a]";
}
?>
The array function inserts the values passed to it into $array. We'll discuss the for loop later on but what happens is if $a is less than the number of values in the array $array the value of $array with an index of $a is displayed. Every time the loop is run the value of $a is incremented.
Associative arrays store value data in keys
<?
$assoc=array("name" => "Jon Smith",
"phone" => "555-555-1234",
"address" => "1010 Home Cir",
"city" => "Atlanta",
"state" => "GA",
"zipcode" => "30060");
$assoc["email"]="jon@smith.com";
foreach ($assoc as $key => $value) {
echo "$key : $value - ";
}
?>
Outputs name : Jon Smith - phone : 555-555-1234 - address : 1010 Home Cir - city : Atlanta - state : GA - zipcode : 30060
- email : jon@smith.comWe'll get into the foreach loop a little later but for every value of $assoc it set the key equal to $key and the value to $value and then used the echo statement to display the values
<?
$info = array("Jon" => array("name" => "Jon Smith","address" => "2 Little St","city" => "Atlanta","state" => "GA",
"zipcode" => "30060"),
"Bob" => array("name" => "Bob Jones","address" => "725 Big Cir","city" => "Miami","state" => "FL",
"zipcode" => "33027"),
"Mark" => array("name" => "Mark Klien","address" => "1315 Riverland Trace","city" => "Boston","state" => "MA",
"zipcode" => "11083");
?>
This stores an array inside an array. To retrieve a value use echo $info["Jon"]["city"] will display Atlanta.
HTML has two methods for passing form variables, POST and GET. GET displays the input in the URL while POST hides it. Variables passed via POST are represented by $_POST["{name of input tag}"] GET uses $_GET[] respectively. Also $_REQUEST[] will take the values of either POST or GET. $_REQUEST also will return variables passed in a URL not from a form.
To learn about form check out the Higherpass Forms Tutorial
To pass an array from a form set the NAME attribute of the input statement with an ending [] to designate it as an array. The example below demonstrates how to do this with the <SELECT> tag.
<?
if (isset($_POST["Submit"])) {
if (is_array($_POST["names"])) {
$a=0;
while (list($key,$value) = each($_POST["names"])) {
echo "$key - $value<BR>";
}
}
}
echo '<FORM METHOD="POST" ACTION="test12.php">';
echo '<SELECT NAME="names[]" SIZE="4" MULTIPLE>';
echo '<OPTION VALUE="4">four';
echo '<OPTION VALUE="3">three';
echo '<OPTION VALUE="2">two';
echo '<OPTION VALUE="1">one';
echo '<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="SUBMIT">';
?>
PHP has a multitude of stored and enviromental variables, they are called superglobals. A complete list is available by calling the phpinfo() function from a page. This tutorial is going to assume you have register globals turned off which is more secure. These variables are stored in the $_SERVER["{key}"] array. Below is a list of common server variables.
REQUEST_URI - Like PHP_SELF except it also holds the any variables being passed through the URL