Home > Article > Backend Development > What does global array mean in php
The full name of global data in PHP is superglobal array or superglobal variable, which is a specially defined array variable in PHP; superglobal array can be accessed anywhere in the script and in any scope, superglobal Array variables are built-in variables that are always available in all scopes.
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
php The full name of the global array is "super global array" or "super global variable". It is a specially defined array variable in PHP. It is called super global array because these arrays can be used anywhere in the script and in any scope. Access, such as functions, classes, files, etc.
Super global array variables are built-in variables that are always available in all scopes
Super global arrays in PHP include the following:
$GLOBALS
A global combined array containing all variables. The name of the variable is the key of the array.
Use var_dump($GLOBALS) to print, you can see that $GLOBALS is a global combination array that contains all.
As of PHP 8.1.0, $GLOBALS is now a read-only copy of the global symbol table. That is, global variables cannot be modified through copies. In previous versions, $GLOBALS arrays and PHP arrays generally behaved differently when passing values, and global variables could be modified via copies.
Before PHP 8.1.0:
$a = 1;$globals = $GLOBALS; // 表面意义的按值复制$globals['a'] = 2; // $GLOBALS['a'] 的值也相应修改 var_dump($a);//运行结果: int(2)
From PHP 8.1.0:
$a = 1;$globals = $GLOBALS; //表面意义的按值复制$globals['a'] = 2; // $GLOBALS['a'] 的值不会改变(不再修改 $a) var_dump($a);//运行结果: int(1)
To restore the previous behavior, iterate over its copy and assign each property back $GLOBALS:
foreach ($globals as $key => $value) { $GLOBALS[$key] = $value;}
$_SERVER
$_SERVER - Server and execution environment information. $_SERVER is an array containing information such as header, path, and script locations. The items in this array are created by the web server.
$_GET
An array of variables passed to the current script via URL parameters. Note: This array is not only effective for requests whose method is GET, but for all requests with query string.
$_POST
The predefined $_POST variable is used to collect data from forms with method="post" value.
When the Content-Type of the HTTP POST request is application/x-www-form-urlencoded or multipart/form-data, the variables will be passed into the current script in the form of an associative array.
Information sent from a form with the POST method is invisible to anyone (will not be displayed in the browser's address bar), and there is no limit on the amount of information sent.
Note: However, by default, the maximum amount of information sent by the POST method is 8 MB (can be changed by setting post_max_size in the php.ini file).
$_REQUEST
Contains an array of $_GET, $_POST and $_COOKIE by default. Due to security issues, it is recommended to avoid using $_REQUEST.
$_COOKIE
An array of variables passed to the current script through HTTP Cookies.
$_SESSION
An array of SESSION variables available to the current script.
$_FILES
An array of projects uploaded to the current script via HTTP POST.
$_ENV
An array of variables passed to the current script through the environment.
These variables are imported from the PHP parser's running environment into the PHP global namespace. Many are provided by shells that support PHP running, and different systems are likely to run different kinds of shells, so a definitive list is impossible. Please check your shell documentation for a list of defined environment variables.
Other environment variables include CGI variables, regardless of whether PHP is running as a server module or a CGI processor.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does global array mean in php. For more information, please follow other related articles on the PHP Chinese website!