Easy Tutorial
❮ Func Array Range Filter Validate Boolean ❯

PHP Super Globals


Super globals were introduced in PHP 4.1.0 and are built-in variables available in all scopes throughout a script.


PHP Super Globals

PHP has several predefined super globals, which are available in all scopes without the need for special declaration. You can use them within functions and classes without additional specification.

PHP Super Globals List:

This section will cover some commonly used super globals; the rest will be introduced in the following sections.


PHP $GLOBALS

$GLOBALS is a PHP super global array that allows access to all variables globally within a PHP script.

$GLOBALS is an associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

Here is an example of how to use the super global $GLOBALS:

Example

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

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

addition(); 
echo $z; 
?>

In the above example, $z is a super global variable within the $GLOBALS array and can be accessed outside the function.


PHP $_SERVER

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

Here is an example of how to use elements from $_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'];
?>

Below is a table listing important elements from the $_SERVER variable:

Element/Code Description
$_SERVER['PHP_SELF'] The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e., included) file. Since PHP 4.3.0, if PHP is running as a command-line script, this variable contains the script name. Earlier versions do not have this element.
$_SERVER['GATEWAY_INTERFACE'] The version of the Common Gateway Interface (CGI) the server is using; for example, "CGI/1.1".
$_SERVER['SERVER_ADDR'] The IP address of the server under which the current script is executing.
$_SERVER['SERVER_NAME'] The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host. (e.g., www.tutorialpro.org)
$_SERVER['SERVER_SOFTWARE'] Server identification string, given in the headers when responding to requests. (e.g., Apache/2.2.24)
$_SERVER['SERVER_PROTOCOL'] The name and revision of the information protocol via which the page was requested; for example, "HTTP/1.0".
$_SERVER['REQUEST_METHOD'] The request method used to access the page; for example, 'GET', 'HEAD', 'POST', 'PUT'.
$_SERVER['REQUEST_TIME'] The timestamp of the start of the request. Available since PHP 5.1.0. (e.g., 1377687496)
$_SERVER['QUERY_STRING'] The query string, if any, via which the page was accessed.
$_SERVER['HTTP_ACCEPT'] The Accept: header from the current request, if there is one.
$_SERVER['HTTP_ACCEPT_CHARSET'] The content of the Accept-Charset header from the current request, if present. For example: "iso-8859-1,*,utf-8".
$_SERVER['HTTP_HOST'] The content of the Host header from the current request, if present.
$_SERVER['HTTP_REFERER'] The address of the previous web page from which a link to the currently requested page was followed (if available). This is determined by the user agent settings and not all user agents set this, some even allow modification of HTTP_REFERER. Therefore, this value is not reliable.
$_SERVER['HTTPS'] Set to a non-empty value if the script was accessed via the HTTPS protocol.
$_SERVER['REMOTE_ADDR'] The IP address from which the user is viewing the current page.
$_SERVER['REMOTE_HOST'] The host name of the user viewing the current page. Reverse DNS lookup does not depend on the user's REMOTE_ADDR.
$_SERVER['REMOTE_PORT'] The port being used on the user's machine to communicate with the web server.
$_SERVER['SCRIPT_FILENAME'] The absolute path of the currently executing script.
$_SERVER['SERVER_ADMIN'] This value is set to the SERVER_ADMIN directive in the Apache server configuration file. If the script is running on a virtual host, this value will be the virtual host's value. (e.g., [email protected])
$_SERVER['SERVER_PORT'] The port on the web server being used. The default value is "80". If using an SSL secure connection, this value is the user-set HTTP port.
$_SERVER['SERVER_SIGNATURE'] A string containing the server version and virtual host name.
$_SERVER['PATH_TRANSLATED'] The base path of the current script on the file system (not the document root). This is the result after the server maps the virtual path to the real path.
$_SERVER['SCRIPT_NAME'] Contains the path of the current script. Useful for pages that need to point to themselves. The __FILE__ constant contains the full path and file name of the current script (e.g., included files).
$_SERVER['SCRIPT_URI'] The URI used to request the current page. For example, "/index.html".

PHP $_REQUEST

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

The following example shows a form with an input field and a submit button. When a user clicks the "Submit" button, the form data is sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file to handle the form data. If you want another PHP file to handle the data, you can modify the specified script file name. Then, we can use the superglobal variable $_REQUEST to collect data from the input field in the form:

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>

PHP $_POST

PHP $_POST is widely used to collect form data submitted with the "method="post" attribute in the HTML form tag.

The following example shows a form with an input field and a submit button. When a user clicks the "Submit" button, the form data is sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file to handle the form data. If you want another PHP file to handle the data, you can modify the specified script file name. Then, we can use the superglobal variable $_POST to collect data from the input field in the form:

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>
</body>
</html>

PHP $_GET

PHP $_GET is also widely used to collect form data after submitting an HTML form with method="get".

$_GET can also collect data sent in the URL.

Assume we have an HTML page that contains a hyperlink with parameters:

<html>
<body>

<a href="test_get.php?subject=PHP&web=tutorialpro.org">Test $GET</a>

</body>
</html>

When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can use the $_GET variable to retrieve this data in the "test_get.php" file.

The following example shows the code for the "test_get.php" file:

Example

<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " @ " . $_GET['web'];
?>

</body>
</html>

Note: To learn more about $_POST and $_GET, visit our PHP Forms section.

❮ Func Array Range Filter Validate Boolean ❯