Loading

PHP: Superglobal Variables

Superglobal Variables

One of the things that makes PHP very easy to use in a web environment are the super global variables that are available to every PHP script at any time during execution. The super globals available are:

  • $GLOBALS
  • $_SERVER
  • $_POST
  • $_GET
  • $_FILES
  • $_COOKIE
  • $_SESSION
  • $_REQUEST
  • $_ENV

These super globals can be accessed at any time to gain valuable information, such as the URL of the website where your script is executing (i.e., $_SERVER['SERVER_NAME']), cookies, a session object, or form data from a Post or Get call.

You can see what any of the variables has in it by typing the following code (changing the global variable name) into a test page on your website:

<?php
print_r($_SERVER);
?>

This will print out all of the key/value pairs that the specified global array variable holds. To access an individual key/value pair in the variable, try this:

<?php
echo($_SERVER['PHP_SELF']);
?>

Some of the most-used global variables are:

$GLOBALS: If you want at any time to see what global variables are available, print out this $GLOBALS variable. It holds all the super global variables in it, along with any global variables you create. We will discuss creating your own global variables in a later lesson.

$_SERVER: At any time, the $_SERVER variable is available to give you information about the server your scripts are running on.

$_POST: On submission of an HTML form, whichever file you choose to process the form has access to the $_POST variable, which contains all the elements and values of the form.

$_COOKIE: This variable holds all cookies that you set within PHP. We will discuss how to use cookies in a later lesson.

$_SESSION: You can store session information for each user in this variable. We will discuss how to use session information in a later lesson.