Home >Backend Development >PHP Tutorial >How to get PHP super global variables (organized and shared)
In the previous article, I brought you "Understand PHP anonymous functions in five minutes (detailed examples)". This article introduces the relevant knowledge of anonymous functions in PHP in detail. This article will take a look at the issues related to super global variables that can be referenced inside functions. I hope it will be helpful to everyone!
Global variables defined outside the function cannot be referenced inside the function, but Sometimes you need to use these global variables within a function. In this case, you need to use super global variables. Super global variables can be referenced inside the function.
Several super global variables are predefined in PHP, which means that they can be referenced in the entire scope of a script. Without special instructions, super global variables can be used in functions and classes.
PHP super global variable:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$ _FILES
$_ENV
$_COOKIE
PHP $GLOBALS
$GLOBALS is a A predefined superglobal array that contains all variables available in the global scope. The name of the variable is the key of the array. $GLOBALS is accessible from the entire scope of a PHP script.
<?php //定义两个全局变量,函数内部不可以访问 $a = 75; $b = 25; //定义函数 function addition() { //将全局变量变为超级全局变量,这样在函数内部就可以正常访问了 $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b']; } //调用函数 addition(); //输出函数内部定义的全局变量 echo $c; ?>Output result:
global
global that is very similar to $GLOBALS, which also allows us to Use global variables defined outside the function inside the function.
global 变量1, 变量2, ...global keyword can be followed by multiple variables as parameters, and multiple variables are separated by "," (comma). At the same time, you should pay attention to some key points when using global:
<?php $a = 1; $b = 2; $c = 3; function demo(){ global $a, $b; echo "变量 a:".$a; echo "<br>变量 b:".$b; echo "<br>变量 c:".$c; } demo(); ?>
Output result:
It can be seen that the result only outputs variables a and b, because the global keyword is only modified within the function There are two, so the variable c is not used successfully.
Through two examples, we can see that compared with global, $GLOBALS has the following differences:
PHP $_SERVER##PHP $_SERVER is an array to be precise, $_SERVER contains Header information, path, script location and other information. The items in this array are created by the web server. Servers may ignore some, and not all items may be available on every server.
<?php //输出当前脚步的文件名 echo "<h3>输出当前脚步的文件名</h3>"; echo $_SERVER['PHP_SELF']; echo "<hr/>"; //当前脚步所在服务器的主机名 echo "<h3>当前脚步所在服务器的主机名</h3>"; echo $_SERVER['SERVER_NAME']; echo "<hr/>"; //当前请求头中 Host echo "<h3>当前请求头中 Host</h3>"; echo $_SERVER['HTTP_HOST']; echo "<hr/>"; //引导用户代理到当前页的前一页的地址(如果存在) echo "<h3>引导用户代理到当前页的前一页的地址(如果存在)</h3>"; echo $_SERVER['HTTP_REFERER']; echo "<hr/>"; //用来检查浏览页面的访问者在用什么操作系统 echo "<h3>用来检查浏览页面的访问者在用什么操作系统</h3>"; echo $_SERVER['HTTP_USER_AGENT']; echo "<hr/>"; //包含当前脚本的路径 echo "<h3>包含当前脚本的路径</h3>"; echo $_SERVER['SCRIPT_NAME']; ?>Output results
Share with everyone , More important elements in the $_SERVER variable:
$_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.
$_SERVER['SERVER_SOFTWARE']
---Server identification string, given in the header information when responding to the request.
$_SERVER['SERVER_PROTOCOL']
---The name and version of the communication protocol when requesting the page.
$_SERVER['REQUEST_METHOD']
---The request method used to access the page.
$_SERVER['REQUEST_TIME']
---The timestamp when the request started. Available since PHP 5.1.0.
$_SERVER['QUERY_STRING']
---query string (query string), if any, use it to access the page.
$_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.
$_SERVER['HTTP_HOST']
---The content of the Host: item in the current request header, if it exists.
$_SERVER['HTTP_REFERER']
---Direct the user agent to the address of the previous page of the current page (if it exists).
$_SERVER['HTTPS']
---If the script is accessed through 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 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.
$_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 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.
$_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 is used to specify the page to be accessed. For example "/index.html".
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get PHP super global variables (organized and shared). For more information, please follow other related articles on the PHP Chinese website!