Analysis of the difference between PHP global variables and super global variables_php skills

WBOY
Release: 2016-05-16 19:55:11
Original
1168 people have browsed 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. So when used inside a function, we often see something like global $a;

Superglobal 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 mistakenly write $_GLOBALS. 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 instructions 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 access directly

This is explained in the manual:

$GLOBALS — References all variables available in global scope References all variables available in global scope

Understand this with an example: Suppose 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 true that the super global variables saved by $GLOBALS can be obtained in another file after being defined in one php file.

Obviously not 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 on this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", " Introductory tutorial on PHP basic syntax", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage》, "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" And "Summary of common database operation skills in PHP"

I hope this article will be helpful to everyone in PHP programming.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!