This paper analyzes the difference between PHP global variables and super global variables. Share it for your reference, as follows:
Global variables are variables defined outside the function. Cannot be used directly in functions. Because its scope will not reach the function. Therefore, when using internal functions, they often see similar global $a;
Hyperglobal variable scope is valid in all scripts. Therefore, it can be used directly in the function. For example, $_GET and $_SERVER are all hyperglobal variables. Hyperglobal variables except $_GET, $_POST, $_SERVER, $_COOKIE, etc. are saved in the $GLOBALS array.Note, don't mistakenly write $_GLOBALS. A little affected by $_POST.
$GLOBAL is a special php custom array. Super global variable. Like $_SERVER, they are all hyperglobal variables.
Search $GLOBALS in the manual to query the specific instructions and usage methods of hyperglobal variables
The variable name is the index of the array. Description: After defining a hyperglobal variable using $GLOBALS['a']. You can access directly using $a
The manual explains this:
$GLOBALS — References all variables available in global scope References to all available variables in the global scope
Based on the example, we understand it as follows: If a variable $a is defined outside the function. Then the value of this variable can be obtained through $GLOBALS['a'] in the function. Therefore, the $GLOBALS array is: all global variables defined by the user.
Why call it a super global variable. Compared with $_POST, it can be used directly in the function. There is no need to use global statements at all. So $GLOBALS is the same principle, but $_POST is saved as a variable passed in the post method. $GLOBALS saves user-defined global variables.
Personal understanding:
The previous misunderstanding of super global variables: super global variables are valid in all scripts, which leads to subtle deviations in understanding. If it is valid under all scripts, is it a super global variable saved by $GLOBALS? It can be obtained in another file after it is defined in one php file.
Obviously this will not be the case. It is probably affected by $_POST because it seems that everyone can access it. Without realizing that you can only access the $_POST data currently processed. It is impossible for two people to submit messages at the same time. How to distinguish the data obtained from the post from the php file. This data can only be from the current thread. Finally, the super global variable can be viewed like this, because it is relative to the global variable. It is one level more than global variables, and global variables cannot act inside the function. It solves this problem. In other languages, global variables can be applied to the function. This is not the case with PHP language design.
If global variables are used directly in the function, then we must provide a variable that can be used directly. The concept of super global variables comes out.
For more information about PHP related content, please check out the topic of this site:Summary of PHP operations and operator usage》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of php operation office documentation skills (including word, excel, access, ppt)》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.