Predefined Variables

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 Array
  1: <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:

  • $_GET contains any variables provided to a script through the GET method.

  • $_POST contains any variables provided to a script through the POST method.

  • $_COOKIE contains any variables provided to a script through a cookie.

  • $_FILES contains any variables provided to a script through file uploads.

  • $_ENV contains any variables provided to a script as part of the server environment.

  • $_REQUEST contains any variables provided to a script via any user input mechanism.

  • $_SESSION contains any variables that are currently registered in a session.


If you're using a version of PHP earlier than 4.1.x and cannot upgrade to a newer version, you must adjust the names of the variables when you're following the scripts in this book. The old names are $HTTP_GET_VARS (for $_GET), $HTTP_POST_VARS (for $_POST), $HTTP_COOKIE_VARS (for $_COOKIE), $HTTP_POST_FILES (for $_FILES), $HTTP_ENV_VARS (for $_ENV), and $HTTP_SESSION_VARS (for $_SESSION). These are not superglobals, however, so you must declare them as such, or pass them as parameters, when using functions.

    Part III: Getting Involved with the Code