Differences between global variables and Super global variables in PHP

Source: Internet
Author: User
Differences between PHP global variables and Super global variables, and differences between php global variables. Differences between PHP global variables and Super global variables: differences between php global variables This article analyzes the differences between PHP global variables and Super global variables. For your reference, the differences between PHP global variables and Super global variables are as follows:

This article analyzes the differences between PHP global variables and Super global variables. We will share this with you for your reference. The details are as follows:

Global variables are defined outside the function. It cannot be used directly in functions. Because its scope will not be inside the function. Therefore, when using the function, we often see global $;

The hyperglobal variable scope is valid in all scripts. Therefore, functions can be used directly. For example, $ _ GET and $ _ SERVER are all super global variables. Hyperglobal variables except $ _ GET, $ _ POST, $ _ SERVER, and $ _ COOKIE are stored in the $ GLOBALS array. Note: do not mistakenly write $ _ GLOBALS. It is somewhat affected by $ _ POST.

$ GLOBAL is a special php custom array. Super global variable. Like $ _ SERVER, they all belong to Super global variables.

Search $ GLOBALS in the manual to query the detailed instructions and usage of the Super global variables.

The variable name is the index of the array. Note: Use $ GLOBALS ['A'] to define a super global variable. You can use $ a for direct access.

This is explained in the manual:

$ GLOBALS-References all variables available in global scope reference all available variables within the global scope

In combination with the example, assume that a variable $ a is defined outside the function. You can use $ GLOBALS ['A'] to obtain the value of this variable in the function. Therefore, the $ GLOBALS array contains all the global variables defined by the user.

Why is it called a super global variable. Compare $ _ POST, which can be used directly in functions. The global statement is not required at all. Therefore, $ GLOBALS works like this, but $ _ POST stores the variables passed in post mode. $ GLOBALS stores user-defined global variables.

Personal understanding:

The previous understanding of Super global variables is not correct: Super global variables are valid in all scripts, which leads to slight misunderstanding. If it is valid in all scripts, is it a super global variable saved by $ GLOBALS? it can be obtained in another file after being defined in one php file.

Obviously, this is not the case. It is estimated that it is affected by $ _ POST because it seems that everyone can access it. You can only access the currently processed $ _ POST data. It is impossible for two people to submit messages at the same time. how is the difference between the post data obtained from the PHP file. This data can only be of the current thread. Finally, the Super global variable can be viewed in this way, because it is relative to the global variable. A global variable cannot be used within a function. It solves this problem. In other languages, global variables can be used within the function. Php language design is not like this.

Global variables are directly used in functions, so you must provide a variable that can be used directly. This introduces the concept of super global variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.