WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Global Variables - Superglobals


Super global variables have been enabled since PHP 4.1.0. They are variables built into the PHP system and are available in all scopes of a script.


PHP super global variables

Several superglobals are predefined in PHP, which means they are available in all scopes of a script. You don't need special instructions to use it in functions and classes.

List of PHP super global variables:

  • $ GLOBALS
  • $ _ SERVER
  • $ _ REQUEST
  • $ _ POST
  • $ _ GET
  • $ _ FILES
  • $ _ ENV
  • $ _ COOKIE
  • $ _ SESSION

In this chapter, we will explain several commonly used super-global variables. The remaining variables will be introduced in the next few chapters.


PHP $ GLOBALS

$ GLOBALS is a super global variable group for PHP, accessible in all scopes of a PHP script.

$ GLOBALS is a global composite array containing all variables. The name of the variable is the key of the array.

The following example shows how to use the super global variable $ 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 superglobal variable in the $ GLOBALS array, which can also be accessed outside the function.


PHP $ _SERVER

$ _ SERVER is an array containing information such as headers, paths, and script locations. The items in this array are created by the web server. There is no guarantee that every server will provide all items; the server may ignore some or provide items that are not listed here.

The following example shows how to use the elements in $ _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']; ?>

The following table lists all the important elements in the $ _SERVER variable:

The
element /code Description
$ _ SERVER ['PHP_SELF'] The filename of the currently executing script, related to document root. For example, using $ _SERVER ['PHP_SELF'] in a script with the address http://example.com/test.php/foo.bar will result in /test.php/foo.bar. The __FILE__ constant contains the full path and file name of the current (eg, included) file. Starting with PHP 4.3.0, this variable will contain the script name if PHP is running in command line mode. This variable is not available in previous versions.
$ _ SERVER ['GATEWAY_INTERFACE'] The version of the CGI specification used by the server; for example, "CGI /1.1".
$ _ SERVER ['SERVER_ADDR'] The IP address of the server where the script is currently running.
$ _ SERVER ['SERVER_NAME'] The host name of the server where the script is currently running. If the script is running on a virtual host, the name is determined by the value set by that virtual host. (E.g. www.runoob.com)
$ _ SERVER ['SERVER_SOFTWARE'] The server identification string, which is given in the header information when responding to the request. (Eg: Apache /2.2.24)
$ _ SERVER ['SERVER_PROTOCOL'] The name and version of the communication protocol when 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 time stamp when the request started. Available since PHP 5.1.0. (Eg: 1377687496)
$ _ SERVER ['QUERY_STRING'] query string, if any, page access through it.
$ _ SERVER ['HTTP_ACCEPT'] The content of the Accept: item in the current request header, if it exists.
$ _ SERVER ['HTTP_ACCEPT_CHARSET'] The content of the Accept-Charset: item in the current request header, if it exists. For example: "iso-8859-1, *, utf-8".
$ _ SERVER ['HTTP_HOST'] The content of the Host: item in the current request header, if it exists.
$ _ SERVER ['HTTP_REFERER'] Guide the user agent to the address of the previous page of the current page, if one exists. Determined by the user agent setting. Not all user agents will set this, and some also provide the ability to modify HTTP_REFERER. In short, the value is not credible. )
$ _ SERVER ['HTTPS'] If the script is accessed via the HTTPS protocol, it is set to a non-null value.
$ _ SERVER ['REMOTE_ADDR'] The IP address of the user viewing the current page.
$ _ SERVER ['REMOTE_HOST'] The host name of the user viewing the current page. DNS reverse resolution does not depend on the user's REMOTE_ADDR.
$ _ SERVER ['REMOTE_PORT'] The port number used by the user's machine to connect to the web server.
$ _ SERVER ['SCRIPT_FILENAME'] The absolute path of the currently executing script.
$ _ SERVER ['SERVER_ADMIN'] This value indicates the SERVER_ADMIN parameter in the Apache server configuration file. If the script runs on a virtual host, the value is the value of that virtual host. (Eg: someone@runoob.com)
$ _ SERVER ['SERVER_PORT'] Port used by the web server. The default value is "80". If using an SSL-secured connection, this value is the HTTP port set by the user.
$ _ SERVER ['SERVER_SIGNATURE'] A string containing the server version and virtual host name.
$ _ SERVER ['PATH_TRANSLATED'] The base path of the file system (non-document root directory) where the current script is located. This is the result of the server mapping the virtual to real path.
$ _ SERVER ['SCRIPT_NAME'] Contains the path of the current script. This is useful when the page needs to point to itself. The __FILE__ constant contains the full path and file name of the current script (such as an include file).
$ _ SERVER ['SCRIPT_URI'] URI specifies the page to be accessed. For example "/index.html".


PHP $ _REQUEST

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

The following example shows a form with an input field and a submit button. When the user submits the form data by clicking the "Submit" button, the form data will be sent to <form>The script file specified in the action attribute in the tag. In this example, we specify a file to process the form data. If you want other PHP files to process the data, you can modify the specified script file name. Then, we can use the super global variable $ _REQUEST to collect input field data 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. Specify this attribute in HTML form tags: "method =" post ".

The following example shows a form with an input field and a submit button. When the user submits the form data by clicking the "Submit" button, the form data will be sent to<form>The script file specified in the action attribute in the tag. In this example, we specify a file to process the form data. If you want other PHP files to process the data, you can modify the specified script file name. Then, we can use the super global variable $ _POST to collect input field data in the form:

Examples

<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>



PHP $_GET

PHP $ _GET is also widely used to collect form data. Specify this attribute in the HTML form tag: "method =" get ".

$ _ GET can also collect data sent in URLs.

Suppose we have a hyperlinked HTML page with parameters:

<html>
<body>

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

</body>
</html>

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

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

Example

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

Tip: If you want to learn more about $ _POST and $ _GET, please visit our PHP Forms section .