PHP Global Variables - Superglobal Variables

Superglobal variables were introduced in PHP 4.1.0 and are built-in variables that are always available in the entire scope.

PHP Global Variables - Superglobal Variables

Many predefined variables in PHP are 'superglobal', which means they are available in the entire scope of a script. They can be accessed without executing global $variable; in functions or methods.

These superglobal variables are:

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

This section will introduce some superglobal variables and will explain other superglobal variables in later chapters.

$GLOBALS — References all variables available in the global scope

The superglobal variable $GLOBALS is used to access global variables at any position in the PHP script (from functions or methods).

PHP stores all global variables in an array named $GLOBALS[index]. The variable name is the key of the array.

The following example demonstrates how to use the superglobal variable $GLOBALS:

Example

<?php 
$x = 75; 
$y = 25;
function addition() { 
  $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
}
addition(); 
echo $z; 
?>

Run Example

In the above example, since z is a variable in the $GLOBALS array, it can also be accessed outside of the function.

PHP $_SERVER

The superglobal variable $_SERVER saves information about headers, paths, and script locations.

The following example demonstrates how to use some elements of $_SERVER:

Example

<?php 
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>

Run Example

The following table lists the most important elements that you can access in $_SERVER:

Element/Code Description
$_SERVER['PHP_SELF'] Returns the filename of the script currently being executed.
$_SERVER['GATEWAY_INTERFACE'] Returns the version of the CGI specification used by the server.
$_SERVER['SERVER_ADDR'] Returns the IP address of the server where the current script is running.
$_SERVER['SERVER_NAME'] Returns the hostname of the server where the current script is running (e.g., www.codew3c.com).
$_SERVER['SERVER_SOFTWARE'] Returns the server identifier string (e.g., Apache/2.2.24).
$_SERVER['SERVER_PROTOCOL'] Returns the name and version of the communication protocol used to request the page (e.g., 'HTTP/1.0').
$_SERVER['REQUEST_METHOD'] Returns the request method used to access the page (e.g., POST).
$_SERVER['REQUEST_TIME'] Returns the timestamp at the start of the request (e.g., 1577687494).
$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed through a query string.
$_SERVER['HTTP_ACCEPT'] Returns the request headers from the current request.
$_SERVER['HTTP_ACCEPT_CHARSET'] Returns the Accept_Charset header from the current request (e.g., utf-8, ISO-8859-1)
$_SERVER['HTTP_HOST'] Returns the Host header from the current request.
$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (unreliable, as not all user agents support it).
$_SERVER['HTTPS'] Whether to query the script through a secure HTTP protocol.
$_SERVER['REMOTE_ADDR'] Returns the IP address of the user browsing the current page.
$_SERVER['REMOTE_HOST'] Returns the hostname of the user browsing the current page.
$_SERVER['REMOTE_PORT'] Returns the port number used by the user's machine to connect to the web server.
$_SERVER['SCRIPT_FILENAME'] Returns the absolute path of the script currently being executed.
$_SERVER['SERVER_ADMIN'] This value indicates the SERVER_ADMIN parameter in the Apache server configuration file.
$_SERVER['SERVER_PORT'] The port used by the web server. The default value is '80'.
$_SERVER['SERVER_SIGNATURE'] Returns the server version and virtual host name.
$_SERVER['PATH_TRANSLATED'] The basic path of the file system where the current script is located (not the document root directory).
$_SERVER['SCRIPT_NAME'] Returns the path of the current script.
$_SERVER['SCRIPT_URI'] Returns the current page URI.

PHP $_REQUEST

PHP $_REQUEST is used to collect data submitted in HTML forms.

The following example demonstrates a form that includes input fields and a submit button. When the user clicks the submit button to submit the form data, the form data will be sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file itself to handle the form data. If you need to use another PHP file to handle the form data, simply change it to the filename of your choice. Then, we can use the superglobal variable $_REQUEST to collect the values of the input fields:

Example

<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php 
$name = $_REQUEST['fname']; 
echo $name; 
?>
</body>
</html>

Run Example

PHP $_POST

PHP $_POST is widely used to collect form data submitted with method="post" in HTML forms. $_POST is also commonly used to pass variables.

The following example demonstrates a form that includes input fields and a submit button. After the user clicks the submit button to submit the data, the form data will be sent to the file specified in the action attribute of the <form> tag. In this example, we specify the file itself to handle the form data. If you want to use another PHP page to handle the form data, please change it to the filename of your choice. Then, we can use the superglobal variable $_POST to collect the values of the input fields:

Example

<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php 
$name = $_POST['fname'];
echo $name; 
?>
</body>
</html>

Run Example

PHP $_GET

PHP $_GET can also be used to collect form data submitted after an HTML form (method="get").

$_GET can also collect data sent in the URL.

Suppose we have a page with hyperlinks containing parameters:

<html>
<body>
<a href="test_get.php?subject=PHP&web=codew3c.com">Test $GET</a>
</body>
</html>

When the user clicks the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and then you can access these values in "test_get.php" through $_GET.

The following example is the code in "test_get.php":

Example

<html>
<body>
<?php 
echo "Learning " . $_GET['web'] . " in " . $_GET['subject'];
?>
</body>
</html>

Run Example

Tip:You will be PHP Forms Learn more about $_POST and $_GET in this section.