Analysis of the difference between PHP global variables and Super global variables _php tips

Source: Internet
Author: User
Tags php language

This paper analyzes the difference between PHP global variable and super global variable. Share to everyone for your reference, specific as follows:

A global variable is a variable defined outside the function. cannot be used directly in a function. Because its scope does not go inside the function. So the global $a is often seen when used inside functions;

The scope of the Super global variable is valid in all scripts. So, the function can be used directly. For example, $_get,$_server are all super global variables. Super global variables, except $_get,$_post,$_server,$_cookie, are saved in the $globals array. Be careful not to write $_globals in error. Somewhat affected by the $_post.

$GLOBAL is a special PHP custom array. Super global variable. Like $_server, all belong to super global variables.

Search $globals in the manual to find out the details of the super global variables and how to use them

The variable name is the index of the array. Description: Use $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 available variables globally

Combine examples to understand this: if a variable $a is defined outside the function. Then you can get the value of this variable in the function by $globals[' a '. So, inside the $GLOBALS array is the user-defined global variable.

Why call it a super global variable. Contrast $_post, which can be used directly in functions. There is no need to use the global statement at all. So $globals is the same principle, except that $_post save is a variable that is passed in post mode. $GLOBALS saved is a user-defined global variable.

Personal Understanding:

The previous understanding of the Super global variable is not correct: The super global variable is valid under all scripts, so the expression leads to subtle deviations in understanding. If it works under all scripts, then the $globals saved Super global variable, which is defined in one PHP file, is also available in another file.

It's obviously not going to be like this. Estimated to be affected by $_post because it seems to be accessible to everyone. Unaware that you can access only the $_post data that is currently processed. It is impossible for two people to submit a message at the same time, PHP file to get to post the data how different. This data can only be the current thread. Finally, the super global variable can look like this because it is relative to the global variable. More than a global variable, the global variable cannot function inside the function. It solves the problem. In other languages, global variables can function inside a function. This is not the case for PHP language design.

Global variables are used directly within a function, so always provide a variable that can be used directly. It comes out. The concept of super global variables.

More interested in PHP related content readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "PHP operation Office Document Skills Summary (including word,excel,access, PPT), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Course", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.