Home > Backend Development > PHP Tutorial > Analysis of the difference between PHP global variables and super global variables, the difference between PHP global variables_PHP tutorial

Analysis of the difference between PHP global variables and super global variables, the difference between PHP global variables_PHP tutorial

WBOY
Release: 2016-07-12 08:55:23
Original
874 people have browsed it

Analysis of the difference between PHP global variables and super global variables, the difference between PHP global variables

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 the super global variable saved by $GLOBALS? After being defined in one php file, it can still be obtained in another 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", "Introduction to PHP basic syntax", "php operation office documentation" Summary of skills (including word, excel, access, ppt)", "Summary of php date and time usage", "Introduction to php object-oriented programming tutorial", "Summary of php string (string) usage", "Introduction to php mysql database operation" Tutorial" and "Summary of Common PHP Database Operation Skills"

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

Articles you may be interested in:

  • PHP Global Instructions for defining global variables
  • Analysis of several methods of using global variables in PHP
  • php Summary of array declaration, traversal, and use of array global variables
  • Summary of PHP super global variable array
  • Analysis of the difference between global variables global and $GLOBALS[] in PHP
  • Understand and use PHP super global variables
  • php global variable scope analysis
  • How to reference global variables within functions in PHP
  • Use php to implement global variables like Application in JSP and ASP
  • Superglobal variables in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117044.htmlTechArticleAnalysis of the difference between PHP global variables and super global variables, the difference between php global variables This article analyzes PHP global variables and super global variables the difference. Share it with everyone for your reference, the details are as follows:...
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