Differences between global and hyper-global variables in PHP

Source: Internet
Author: User
This article mainly introduces the PHP global variable and the Super global variable Difference, the detailed analysis tells the PHP global variable and the Super global variable concept, the usage and the use difference, the need friend can refer to the next

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

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

The scope of the hyper-global variable is valid for all scripts. Therefore, the function can be used directly. For example, $_get,$_server are super global variables. Hyper-Global variables other than $_get,$_post,$_server,$_cookie are saved in the $globals array. note, do not mistakenly write $_globals. A little affected by the $_post.

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

Search the manual for $globals to query for specific instructions and how to use the hyper-global variables

The variable name is the index of the array. Description: After defining a super global variable with $globals[' a ']. You can use a $ A direct access

This is explained in the manual:

$GLOBALS-references All variables available in global scope references all available variables at global scope

This is understood in conjunction with an example: if a variable $ A is defined outside the function. Then the value of this variable can be obtained through $globals[' a '] in the function. So, $GLOBALS array inside is: All user-defined global variables.

Why call it a super global variable. Compared to $_post, it can be used directly in the function. There is no need to use the global statement at all. So $globals is the same principle, just $_post save is the post way of the variables passed. A user-defined global variable is $GLOBALS saved.

Personal Understanding:

The previous understanding of the Super global variable is not correct: The super global variable is valid under all scripts, which leads to subtle deviations in understanding. If it is valid under all scripts, then it is not a $globals saved Super global variable that is defined in one PHP file and can be obtained in another file.

Obviously that's not the case. The estimate was influenced by the $_post, as it seemed to be accessible to all. Unaware that you can only access the $_post data that is currently processed. It is impossible for two people to submit a message at the same time, PHP files to get to the post 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, global variables cannot function inside the function. It solves the problem. In other languages, global variables can function inside a function. This is not the case with PHP language design.

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

Related Article

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.