$myarray["R"] = 'red';
$myarray["G"] = 'green';
$myarray["B"] = 'blue';
Now, to pass through the array, you would do something like the following:
while (list ($abbrev, $color) = each ($myarray)) {
echo "Element $abbrev is $color<br>\n";
}
There's a couple of new things here: there's list, which is a language construct that assigns elements
of an array across a list of variables (in this case the array consists of a pair of keys and values, not the
entire example array); and there's each, which is a function for single-stepping through an array.
Note In PHP, arrays have an internal pointer. You can use this pointer to keep a note
of where you are in an array.
This is handy because it means that you're not forced to use a separate variable
in simple cases.
There are corresponding functions—each(), next(), prev(), and
array_walk()—that give you ways of single-stepping through the array,
invisibly moving the internal pointer each time.
each takes the name of an array as argument, returns the key and value of the current element
(according to the internal pointer), and then moves the pointer on by one. list($key, $value), or
something similar to what you used previously, is a convenient way to get at the key=value pair result.
You've only covered single-dimension arrays in this introductory guide. However, it's worth noting that
PHP has the ability to handle multidimensional arrays. It also has a good range of functions for array
processing, such as for sorting. It's worth exploring other PHP resources for more details on the
possibilities available.
PHP Meets MySQL
PHP has a range of sophisticated functions for interfacing with MySQL. Look at what happens when a client
makes a request to a PHP-enabled Web server where some interaction will occur with a MySQL database:
The server receives and reads the request from the client browser.
The server locates the requested page on the Web server.
The server executes any instructions given by the embedded PHP code.
PHP queries the MySQL database server via an API and compiles the result.
The Web server sends the resulting page to the client.
PHP comprises a considerable suite of functions for interfacing with MySQL database servers. You can
create powerful applications using just a small subset of them (as you'll see in a moment). For
reference, the following is a summary of the full function list.
Table 15.1 MySQL Functions
Function name Action
Connecting and
Disconnecting
mysql_connect
Opens a connection to
a MySQL server
mysql_pconnect (^) Opens a persistent
connection to a MySQL
server
mysql_select_db
Selects a MySQL
database
mysql_close (^) Closes a MySQL
connection
mysql_change_user (^) Changes the identity of