Understand global variables in PHP and global variables. A comprehensive understanding of global variables in PHP, a comprehensive understanding of global variables in PHP, pre-defined several Super global variables (superglobals), which means that they fully understand the global variables in PHP in all scopes of a script, comprehensive understanding of global variables
Several Super global variables (superglobals) are pre-defined in PHP, which means they are available in all scopes of a script. You can use it in functions and classes without special instructions.
PHP Super global variable list:
• $ GLOBALS
• $ _ SERVER
• $ _ REQUEST
• $ _ POST
• $ _ GET
• $ _ FILES
• $ _ ENV
• $ _ COOKIE
• $ _ SESSION
This section describes several frequently-used Super global variables. we will introduce other variables in the following sections.
PHP $ GLOBAL
$ GLOBAL is a super GLOBAL variable group of PHP, which can be accessed in all scopes of a PHP script.
$ GLOBAL is a GLOBAL combination array containing all variables. The variable name is the key of the array.
The following example describes how to use the Super GLOBAL variable $ GLOBAL:
<?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. It is not guaranteed that each server provides all projects. the server may ignore some projects 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 "
";echo $_SERVER['SERVER_NAME'];echo "
";echo $_SERVER['HTTP_HOST'];echo "
";echo $_SERVER['HTTP_REFERER'];echo "
";echo $_SERVER['HTTP_USER_AGENT'];echo "
";echo $_SERVER['SCRIPT_NAME'];?>
The following table lists all the important elements in the $ _ SERVER variable:
Element/code |
Description |
$ _ SERVER ['php _ SELF '] |
The file name of the currently executed script, which is related to document root. For example, using $ _ SERVER ['php _ SELF '] in a script with the address of the http://example.com/test.php/foo.bar will get/test. PHP/foo. bar. The _ FILE _ constant contains the complete path and FILE name of the current (such as include) FILE. Starting from PHP 4.3.0, if PHP runs in command line mode, this variable will contain the script name. The variable is unavailable in earlier versions. |
$ _ SERVER ['gateway _ interface'] |
The CGI standard version 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 on a VM, the name is determined by the value set by the VM. (For example, www.manongjc.com) |
$ _ SERVER ['server _ soft'] |
The server ID string, which is provided in the header information in the response request. (For example, Apache/2.2.24) |
$ _ SERVER ['server _ protocol'] |
The name and version of the communication protocol on the request page. For example, "HTTP/1.0 ". |
$ _ SERVER ['request _ method'] |
The request method used to access the page. for example, "GET", "HEAD", "POST", and "PUT ". |
$ _ SERVER ['request _ time'] |
The start time of the request. Available from PHP 5.1.0. (For example, 1377687496) |
$ _ SERVER ['query _ string'] |
Query string (query string). If yes, use it to access the page. |
$ _ SERVER ['http _ ACCEPT '] |
Accept in the current request header: the content of the item, if any. |
$ _ SERVER ['http _ accept_charset'] |
Accept-Charset in the current request header: content of the item, if any. Example: "ISO-8859-1, *, UTF-8 ". |
$ _ SERVER ['http _ host'] |
Host in the current request header: the content of the item, if any. |
$ _ SERVER ['http _ referer'] |
Directs the user to the address of the previous page of the current page (if any ). Determined by the user agent settings. Not all user proxies set this item, and some also provide the HTTP_REFERER modification function. In short, this value is not credible .) |
$ _ SERVER ['https'] |
If the script is accessed through the HTTPS protocol, it is set to a non-null value. |
$ _ SERVER ['remote _ ADDR '] |
The IP address of the user who browses the current page. |
$ _ SERVER ['remote _ host'] |
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 to connect to the Web server on your machine. |
$ _ SERVER ['script _ filename'] |
The absolute path of the current script. |
$ _ SERVER ['server _ admin'] |
This value specifies the SERVER_ADMIN parameter in the Apache server configuration file. If the script runs on a VM, this value is the value of that VM. (Such as: someone@manongjc.com) |
$ _ SERVER ['server _ port'] |
The port used by the Web server. The default value is 80 ". If SSL secure connection is used, this value is the HTTP port set by the user. |
$ _ SERVER ['server _ SIGNATURE '] |
A string that contains the server version and virtual host name. |
$ _ SERVER ['path _ TRANSLATED '] |
The basic path of the file system (non-document root directory) where the current script is located. This is the result of a virtual image to a real path on the server. |
$ _ SERVER ['script _ name'] |
The path that contains the current script. This is useful when the page needs to point to itself. The _ FILE _ constant contains the complete path and FILE name of the current script (for example, containing a FILE. |
$ _ SERVER ['script _ URI '] |
URI is used to specify the page to access. For example, "/index.html ". |
PHP $ _ REQUEST
PHP $ _ REQUEST is used to collect data submitted by HTML forms.
The following example shows a form for the input field and the submit button (submit ). When you click "Submit" to Submit the form data, the form data is sent to <? Php $ name = $ _ REQUEST ['fname']; echo $ name;?>
PHP $ _ POST
PHP $ _ POST is widely used to collect form data. specify this attribute in the HTML form tag: "method =" post ".
The following example shows a form for the input field and the submit button (submit ). When you click "Submit" to Submit the form data, the form data is sent to <? Php $ name = $ _ POST ['fname']; echo $ name;?>
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 from URLs.
Suppose we have a hyperlink HTML page containing parameters:
Test $GET
When you click "Test $ GET", parameters "subject" and "web" are sent to "test_get.php ", you can use the $ _ GET variable in the "test_get.php" file to obtain the data.
The following example shows the code of the "test_get.php" file:
<?php echo "Study " . $_GET['subject'] . " at " . $_GET['web'];?>
Tip:If you want to learn