PHP super global variables
Super global variables were enabled after PHP 4.1.0. They are variables that come with the PHP system and are available in the entire scope of a script.
PHP Super Global Variables
Several super global variables (superglobals) are predefined in PHP, which means that they are Available in all scopes of a script. You can use it in functions and classes without special instructions.
What are the super global variables in php?
PHP super global variable list:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
In this chapter we will explain a few Commonly used super global variables, we will introduce the remaining variables in the next few chapters.
PHP $GLOBALS
$GLOBALS is a super global variable group of PHP, in the entire scope of a PHP script All can be accessed.
$GLOBALS is a global combined array containing all variables. The name of the variable is the key of the array.
The following example introduces 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 super global variable in the $GLOBALS array, which is the same Can be accessed outside the function.
PHP $_SERVER
$_SERVER is a file that contains information such as header, path, and script Array of information such as location (script locations). The items in this array are created by the web server. There is no guarantee that every server will offer all items; servers may ignore some, or serve items 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 important elements in the $_SERVER variable:
Element/Code | Description |
$_SERVER['PHP_SELF'] | Currently executing script The file name is related to the document root. For example, using $_SERVER['PHP_SELF'] in a script at http://example.com/test.php/foo.bar will result in /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. containing) file. Starting with PHP version 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 for that virtual host. (For example: www.php.cn) |
$_SERVER['SERVER_SOFTWARE'] | The server identification string is given in the header information when responding to the request. (For example: Apache/2.2.24) |
$_SERVER['SERVER_PROTOCOL'] | The name and version of the communication protocol when requesting the page. 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 when the request started. Available since PHP 5.1.0.(For example: 1377687496) |
$_SERVER['QUERY_STRING'] | query string (query string), if any, page access is performed 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'] | Directs the user agent to the address of the page preceding the current page (if one exists). Determined by user agent settings. Not all user agents will set this item, and some also provide the function of modifying HTTP_REFERER. In short, the value is not trustworthy. ) |
$_SERVER['HTTPS'] | Set to a non-empty value if the script is accessed via the HTTPS protocol. |
$_SERVER['REMOTE_ADDR'] | The IP address of the user browsing the current page. |
$_SERVER['REMOTE_HOST'] | The host name of the user browsing the current page. DNS reverse resolution does not depend on the user's REMOTE_ADDR. |
$_SERVER['REMOTE_PORT'] | The port number used on 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 specifies the SERVER_ADMIN parameter in the Apache server configuration file. If the script is running on a virtual host, this value is that of that virtual host. (For example: someone@php.cn) |
Used by the Web server port. The default value is "80". If using SSL secure connection, this value is the HTTP port set by the user. | |
A string containing the server version and virtual host name. | |
The base path of the file system (not the document root directory) where the current script is located. This is the result after the server has been imaged from a virtual to real path. | |
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). | |
URI is used to specify the page to be accessed. For example "/index.html". |