Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

PHP

Learn step by step



PHP Global Variables - Superglobals

The PHP superglobal variables are:

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

  • $_GET The $_GET Superglobal represents data sent to the PHP script in a URL. This applies both to directly accessed URLs (e.g., http://www.tuton.com/page.php?id=2) and form submissions that use the GET method.
  • $_POST The $_POST Superglobal represents data sent to the PHP script via HTTP POST. This is normally a form with a method of POST.
  • $_COOKIE The $_COOKIE Superglobal represents data available to a PHP script via HTTP cookies.
  • $_REQUEST The $_REQUEST Superglobal is a combination of $_GET, $_POST, and $_COOKIE.
  • $_SESSION The $_SESSION Superglobal represents data available to a PHP script that has previously been stored in a session.
  • $_SERVER The $_SERVER Superglobal represents data available to a PHP script from the Web server itself. Common uses of $_SERVER is to refer to the current PHP script ($_SERVER['PHP_SELF']), the path on the server to that script, the host name, and so on. (The image at right shows some sample $_SERVER values, which can be easily viewed in NuSphere's PhpED's Globals pane.)
  • $_ENV The $_ENV Superglobal represents data available to a PHP script from the environment in which PHP is running.
  • $_FILES The $_FILES Superglobal represents data available to a PHP script from HTTP POST file uploads. Using $_FILES is the currently preferred way to handle uploaded files in PHP.

PHP $GLOBALS

$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods).
PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.


<?php
$x = 75;
$y = 25;

function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

addition();
echo $z;
?>
PHP $_SERVER

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.


<?php
//Obtain user IP address
$ip = $_SERVER['REMOTE_ADDR'];
// Obtain browser
$browser = $_SERVER['HTTP_USER_AGENT'];
// Obtain user system language
$language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// Obtain the URL of the page that they came from
$referingURL = $_SERVER['HTTP_REFERER'];
// Obtain the page they are currently on
$currentPage = $_SERVER['REQUEST_URI'];

// Now show all of that information on page
echo "$ip <br />";
echo "$browser <br />";
echo "$language <br />";
echo "$referingURL <br />";
echo "$currentPage <br />";
?>
PHP $_REQUEST

This variable contains the content of both $_GET, $_COOKIE, and $_POST.
This can get the values passed from the form with both the GET and POST methods.


<form action="myphp.php" method="POST">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
<input type="submit" />
</form>

<?php
echo "Firstname: " $_REQUEST["firstname"]; . <br />;
echo "Lastname: " $_REQUEST["lastname"];
?>

OR

<form action="myphp.php" method="GET">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
<input type="submit" />
</form>

<?php
echo "Firstname: " . $_REQUEST["firstname"] . "<br />";
echo "Lastname: " . $_REQUEST["lastname"] ;
?>


Related Videos