Super global variables are enabled after PHP 4.1.0. They are variables that come with PHP systems 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 to specify it, it can be used in functions and classes.
PHP super global variable list:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
In this chapter, we will explain several commonly used super global variables, and we will introduce the remaining variables in the next few chapters.
PHP $GLOBALS
$GLOBALS is a super global variable group of PHP that can be accessed in all scopes of a PHP script.
$GLOBALS is a global combination array containing all variables. The name of a variable is the key of the array.
The following example describes how to use the super global variable $GLOBALS:
<?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 can also be accessed outside the function.
PHP $_SERVER
$_SERVER is an array containing information such as header, path, and script locations. The items in this array are created by the web server. There is no guarantee that every server will provide all projects; the server may ignore some, or provide some projects that are not listed here.
The following example shows how to use elements in $_SERVER:
<?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 important elements in all $_SERVER variables:
Elements/code | describe |
$_SERVER['PHP_SELF'] | The file name of the currently executing script is related to document root. For example, using $_SERVER['PHP_SELF'] in a script with address // will get //. The __FILE__ constant contains the full path and file name of the current (for example, including) file. Starting with PHP version 4.3.0, if PHP is running in command line mode, this variable will contain the script name. The variable was 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 runs in a virtual host, the name is determined by the value set by that virtual host. (like: ) |
$_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 at the start of the request. Available from PHP 5.1.0. (For example: 1377687496) |
$_SERVER['QUERY_STRING'] | query string, if any, page access through it. |
$_SERVER['HTTP_ACCEPT'] | The contents of the Accept: item in the current request header, if it exists. |
$_SERVER['HTTP_ACCEPT_CHARSET'] | The contents 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'] | Boot the user agent to the address of the previous page of the current page (if it exists). Determined by the user agent settings. Not all user agents will set this item, and some also provide the function of modifying HTTP_REFERER. In short, this value is not credible. ) |
$_SERVER['HTTPS'] | If the script is accessed via the HTTPS protocol, it is set to a non-empty value. |
$_SERVER['REMOTE_ADDR'] | The IP address of the user browsing the current page. |
$_SERVER['REMOTE_HOST'] | The hostname of the user who browses the current page. DNS reverse resolution does not depend on the user's REMOTE_ADDR. |
$_SERVER['REMOTE_PORT'] | The port number used to connect to the web server on the user's machine. |
$_SERVER['SCRIPT_FILENAME'] | The absolute path to the current execution script. |
$_SERVER['SERVER_ADMIN'] | This value specifies 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. (For example: someone@) |
$_SERVER['SERVER_PORT'] | The port used by the web server. The default value is "80". If using SSL secure 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 basic path to the file system (not the root directory of the document) where the current script is located. This is the result after the server performs a virtual to the real path image. |
$_SERVER['SCRIPT_NAME'] | Contains the path to the current script. This is very 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 containing a file). |
$_SERVER['SCRIPT_URI'] | URI is used to specify the page to access. For example "/". |
PHP $_REQUEST
PHP $_REQUEST is used to collect data submitted by HTML form.
The following example shows a form with an input field and a submit button. When the user submits form data by clicking the "Submit" button, the form data is sent to the script file specified in the action attribute in the <form> tag. In this example, we specify the file to process the form data. If you want other PHP files to process the data, you can modify the specified script file name. We can then use the super global variable $_REQUEST to collect input field data in the form:
<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. The attribute specified in the HTML form tag is "method="post".
The following example shows a form with an input field and a submit button. When the user submits form data by clicking the "Submit" button, the form data is sent to the script file specified in the action attribute in the <form> tag. In this example, we specify the file to process the form data. If you want other PHP files to process the data, you can modify the specified script file name. We can then use the super global variable $_POST to collect input field data in the form:
<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. The attribute specified in the HTML form tag is "method="get".
$_GET can also collect data sent in the URL.
Suppose we have a hyperlink HTML page containing parameters:
<html> <body> <a href="test_get.php?subject=PHP&web=" rel="external nofollow" >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 the $_GET variable in the "test_get.php" file to get this data.
The following example shows the code for the "test_get.php" file:
<html> <body> <?php echo "Study " . $_GET['subject'] . " @ " . $_GET['web']; ?> </body> </html>
The above is the detailed content of the summary of PHP super global variables. For more information about PHP super global variables, please pay attention to my other related articles!