Differences between PHP global variables and super global variables
This article analyzes the differences between PHP global variables and super global variables. We will share this with you for your reference. The details are as follows:
Global variables are defined outside the function. It cannot be used directly in functions. Because its scope will not be inside the function. Therefore, when using the function, we often see global $;
The hyperglobal variable scope is valid in all scripts. Therefore, functions can be used directly. For example, $ _ GET and $ _ SERVER are all super global variables. Hyperglobal variables except $ _ GET, $ _ POST, $ _ SERVER, and $ _ COOKIE are stored in the $ GLOBALS array. Note: Do not mistakenly write $ _ GLOBALS. It is somewhat affected by $ _ POST.
$ GLOBAL is a special php custom array. Super global variable. Like $ _ SERVER, they all belong to super global variables.
Search $ GLOBALS in the manual to query the detailed instructions and usage of the Super global variables.
The variable name is the index of the array. Note: use $ GLOBALS ['a'] to define a super global variable. You can use $ a for direct access.
This is explained in the manual:
$ GLOBALS-References all variables available in global scope reference all available variables within the global scope
In combination with the example, assume that a variable $ a is defined outside the function. You can use $ GLOBALS ['a'] to obtain the value of this variable in the function. Therefore, the $ GLOBALS array contains all the global variables defined by the user.
Why is it called a super global variable. Compare $ _ POST, which can be used directly in functions. The global statement is not required at all. Therefore, $ GLOBALS works like this, but $ _ POST stores the variables passed in post mode. $ GLOBALS stores user-defined global variables.
Personal Understanding:
The previous understanding of super global variables is not correct: Super global variables are valid in all scripts, which leads to slight misunderstanding. If it is valid in all scripts, is it a super global variable saved by $ GLOBALS? It can be obtained in another file after being defined in one PHP file.
Obviously, this is not the case. It is estimated that it is affected by $ _ POST because it seems that everyone can access it. You can only access the currently processed $ _ POST data. It is impossible for two people to submit messages at the same time. How is the difference between the post data obtained from the PHP file. This data can only be of the current thread. Finally, the super global variable can be viewed in this way, because it is relative to the global variable. A global variable cannot be used within a function. It solves this problem. In other languages, global variables can be used within the function. Php language design is not like this.
Global variables are directly used in functions, so you must provide a variable that can be used directly. This introduces the concept of super global variables.