Home > Backend Development > PHP Tutorial > The difference between global and superglobal variables in PHP

The difference between global and superglobal variables in PHP

巴扎黑
Release: 2023-03-07 21:50:02
Original
1304 people have browsed it

This article mainly introduces the difference between PHP global variables and super global variables. A more detailed analysis describes the concepts, usage and differences of PHP global variables and super global variables. Friends in need can refer to it

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. Therefore, when used inside a function, we often see something like global $a;

The 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, do not 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 specific 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 in the global scope Variables available within the scope

can be understood with examples: If a variable $a is defined outside the function. 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:

The 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.

The above is the detailed content of The difference between global and superglobal variables in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template