Before you actually build a form and use it to acquire data, you must make a small detour and look again at global variables. You first met global variables in Hour 6, "Working with Functions." To refresh your memory, a global variable is any variable declared at the top level of a script that is, declared outside of any function. All functions are made available in a built-in associative array named $GLOBALS. This is useful in Listing 9.1 because we can take a peek at all of our script's global variables with a single loop. Listing 9.1 Looping Through the $GLOBALS Array1: <html> 2: <head> 3: <title>Listing 9.1 Looping through the $GLOBALS array</title> 4: </head> 5: <body> 6: <?php 7: $user1 = "Bob"; 8: $user2 = "Harry"; 9: $user3 = "Mary"; 10: foreach ($GLOBALS as $key=>$value) { 11: print "\$GLOBALS[\"$key\"] == $value<br>"; 12: } 13: ?> 14: </body> 15: </html> Put these lines into a text file named listing9.1.php, and place that file in your Web server document root. When you access this script through your Web browser, it should look something like Figure 9.1 (with your own values, of course). Figure 9.1. Output of Listing 9.1.
In this listing, we declare three variables (lines 7-9) and then loop through the built-in $GLOBALS associative array (lines 10 and 11), writing both array keys and values to the browser. In the output, we can locate the variables we defined (look toward the bottom of your screen), but we also see an awful lot more in addition to our variables. PHP automatically defines global variables that describe both the server and client environments. The availability of these variables varies according to your system, server, and configuration, but they can be immensely useful. PHP has several predefined variables called superglobals, which essentially means that they're always present and available in your scripts. Each of the following superglobals is actually an array of other variables:
|