Home > Article > Backend Development > Analysis of the difference between PHP global variables and super global variables PHP definition variables PHP environment variables PHP variable functions
This article analyzes the difference between PHP global variables and super global variables. Share it with everyone for your reference, the details are as follows:
Global variables are variables defined outside the function. Cannot be used directly in functions. Because its scope does not go inside the function. So when used inside a function, we often see something like global $a;
Super global variable scope is valid in all scripts. Therefore, the function can be used directly. For example, $_GET and $_SERVER are super global variables. Superglobal variables other than $_GET, $_POST, $_SERVER, $_COOKIE, etc. are stored in the $GLOBALS array. Note, don’t write $_GLOBALS by mistake. A bit affected by $_POST.
$GLOBAL is a special php custom array. Super global variables. Like $_SERVER, they are all super-global variables.
Search $GLOBALS in the manual to find out the detailed description and usage of superglobal variables
The variable name is the index of the array. Description: After using $GLOBALS['a'] to define a super global variable. You can use $a to directly access
This is explained in the manual:
$GLOBALS — References all variables available in global scope References all variables available in the global scope
Understand with examples: If defined outside the function A variable $a. Then you can use $GLOBALS['a'] to get the value of this variable in the function. Therefore, inside the $GLOBALS array are: all user-defined global variables.
Why it is called super global variable. Compared with $_POST, it can be used directly in the function. There is no need to use the global statement at all. So $GLOBALS has the same principle, except that $_POST saves variables passed in the post method. $GLOBALS stores user-defined global variables.
Personal understanding:
My previous understanding of super global variables was incorrect: super global variables are valid in all scripts. This statement led to subtle deviations in understanding. If it is valid under all scripts, is it the super global variable saved by $GLOBALS? After being defined in one php file, it can still be obtained in another file.
Obviously that won’t be the case. It is probably affected by $_POST, because it seems to be accessible to everyone. Didn't realize that you can only access the currently processed $_POST data. It is impossible for two people to submit messages at the same time. How to distinguish the post data obtained from the php file. This data can only belong to the current thread. Finally, super global variables can be viewed this way because they are relative to global variables. One level above global variables, global variables cannot be used inside functions. It solves the problem. In other languages, global variables can be used inside functions. This is not the case with PHP language design.
Global variables are used directly within the function, so you must provide a variable that can be used directly. Here comes the concept of super global variables.
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax tutorial", "PHP operating office document skills" Summary (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php+mysql database operation introduction" Tutorial" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.
The above has introduced the analysis of the difference between PHP global variables and super global variables, including global variables and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.