Scope and difference between PHPGlobal and $ GLOBALS variables

Source: Internet
Author: User
There are many variables in php, such as common variables and Global variables (Global and $ GLOBALS). This article will introduce the usage differences between Global and $ GLOBALS in php. global, Global variable PHPGlobal change... there are many variables in php, such as common variables and Global variables (Global and $ GLOBALS). This article will introduce the usage differences between Global and $ GLOBALS in php.

Global, Global variable

PHP Global variables will find many problems in practical applications that need to be constantly improved. in this article, we provide some specific solutions to the problems that occur in PHP Global variables. the usage skills of PHP hack are described in detail, and the code is used to implement php gtk to write a text viewer, advantages and disadvantages of PHP in website development.

How to correctly implement PHP function extension

PHP error_log () function to process error logs

1: PHP Global variable defines Global variables. However, this Global variable is not applied to the entire website, but to the current page, including all files of include or require.

The instance code is as follows:

$ A = 123; function aa () {Global $ a; // if $ a is not defined as a global variable, the function body cannot access $ a's echo $ ;} aa ();

Summary:

PHP Global variables defined in the function body can be used in vitro. global variables defined in the function body cannot be used in the function body,

The instance code is as follows:

$ Glpbal $ a; $ a = 123; function f () {echo $ a; // error ,}

Take a look at the following example. the instance code is as follows:

Function f () {global $ a; $ a = 123;} f (); echo $ a; // correct, you can use

2: Analysis of PHP Global variables:

Question: In config. inc. php defines some variables ($ a). in other files, the function external include ("config. inc. php "), the function needs to use these variables $ a internally. If no declaration is made, echo $ a cannot print anything. therefore, global $ a is declared, but there are many functions and many variables. I can't keep repeating such statements, can I? Please advise if you have any good solutions.

Answer1: first in config. inc. define a constant in php: define (constant name, constant value), and then require 'config. inc. php, and then you can directly use this constant in this file.

Answer2: I also have a way to define an array, such as $ x [a] and $ x. then, we only need to declare global $ x.

Answer3: I tried your method. no.

Answer4: modify your php. ini file.

Set the PHP Global variable to on. let's look at the complex points below:

The instance code is as follows:

// A. php file
 
// B. php file
 

Why is the output 0 ?!!

In user-defined functions, a local function range will be introduced. any variables used in the function will be limited to the local function scope by default (including the variables in the include and require imported files )!

Explanation:. test_Global in the PHP file is a defined third-party function, which is imported into B using include. the global variable of $ a in the PHP file, so $ a is restricted to the range of local functions of Test_Global, so B. $ a in the PHP file takes effect in Test_Global, instead of. php ....

Solution:

1. the code for flushing out a local function instance is as follows:

// A. php file
 
// B. php file
 

Differences between global and $ GLOBALS

In php, global and $ GLOBALS are not just different in terms of writing. The difference between the two is still very big. you need to pay attention to it in actual applications!

Let's take a look at the following instance code:

 

Execution result: 0 5

How can this happen? Shouldn't it be 2 or 5? How can one 0 and one 5 appear?

Well, let's keep the above questions for an in-depth analysis of the principles of $ GLOBALS and global! We all know that variables are actually the "code" in the corresponding physical code, and the $ GLOBALS reference to the php Manual is explained as follows:

Global variable: $ GLOBALS. note: $ GLOBALS is applicable to PHP 3.0.0 and later versions. an array composed of all defined global variables. the variable name is the index of the array. this is a "superglobal" or can be described as an automatic global variable. that is to say, $ var1 and $ GLOBALS ['var1'] in the above code refer to the same variable, rather than two different variables! Next, let's analyze what global has done?

Global interpretation of the reference php manual:

If a global variable is assigned to a reference within a function, the reference is only visible within the function. you can avoid this by using the $ GLOBALS array. we all know that all the variables produced by functions in php are private variables of the function, so the variables generated by the global keyword certainly cannot escape this rule. why do we say this,

See the following code:

 

Execution result: 1

Why does output 1? Didn't $ a have been given to unset? Unset failed? Php bug?

None of them. in fact, unset takes effect. it is to drop $ a from the test function to unset. you can add print $ a; to the test function!

Return to Example 1 above and check the code "$ var2 = & $ var1;" in test_global. the above is a reference value assignment operation, that is, $ var2 will point to the physical memory address pointed to by var1. Therefore, after test_global function is executed in example 1, variable changes only have an effect on the local part of the function, external function $ var2 points to the physical memory address and does not change, or it itself. now, we can understand why $ var2 is 0 and $ var3 is 5 after Example 1 is executed!

So we come to the conclusion that the difference between global and $ GLOBALS [] in the function is:

Global generates an alias variable pointing to the external variable of the function, instead of the real external variable of the function. Once the pointing address of the alias variable is changed, unexpected situations may occur, example 1.

$ GLOBALS [] is indeed an external variable, and the internal and external functions are always consistent. you can better understand the following two columns:

Global:

The instance code is as follows:

 

Output: someting

$ GLOBALS []:

 

Output: Null

After understanding the above ideas, is there any dizzy situation?

The instance code is as follows:

 

The output is "3". The global variables $ a and $ B are declared in the function. all referenced variables of any variable point to the global variable.

Why isn't it 2? isn't it not affected outside the function? please note that $ B does not reference and modify in the function, but the modified $ B points to the physical memory value, therefore, the external input is 3.

In php, global and $ GLOBALS are not just different in terms of writing. The difference between the two is still very big. you need to pay attention to it in actual applications!

Let's take a look at the following PHP code example:

 

Execution result: 0 5

How can this happen? Shouldn't it be 2 or 5? How can one 0 and one 5 appear?

Well, let's keep the above questions for an in-depth analysis of the principles of $ GLOBALS and global! We all know that variables are actually the "code" in the corresponding physical code. Reference $ GLOBALS in the php manual:

Global variable: $ GLOBALS. note: $ GLOBALS is applicable to PHP 3.0.0 and later versions.

An array composed of all defined global variables. the variable name is the index of the array. this is a "superglobal" or can be described as an automatic global variable. that is to say, $ var1 and $ GLOBALS ['var1'] in the above code refer to the same variable, rather than two different variables!

Next, let's analyze what global has done?

Global interpretation of the reference php manual:

If a global variable is assigned to a reference within a function, the reference is only visible within the function. you can avoid this by using the $ GLOBALS array. we all know that all the variables produced by functions in php are private variables of the function, so the variables generated by the global keyword certainly cannot escape this rule. why do we say this? let's look at the following code:

The instance code is as follows:

 

Execution result: 1

Why does output 1? Didn't $ a have been given to unset? Unset failed? Php bug?

None of them. in fact, unset takes effect. it is to drop $ a from the test function to unset. you can add print $ a; to the test function!

Return to Example 1 above and check the code "$ var2 = & $ var1;" in test_global. the above is a reference value assignment operation, that is, $ var2 will point to the physical memory address pointed to by var1. Therefore, after test_global function is executed in example 1, variable changes only have an effect on the local part of the function, external function $ var2 points to the physical memory address and does not change, or it itself.

Now, we can understand why $ var2 is 0 and $ var3 is 5 after Example 1 is executed!

So we come to the conclusion that the difference between global and $ GLOBALS [] in the function is:

Global generates an alias variable pointing to the external variable of the function, instead of the real external variable of the function. Once the pointing address of the alias variable is changed, unexpected situations may occur, example 1.

$ GLOBALS [] is indeed an external variable, and the internal and external functions are always consistent. you can better understand the following two columns:

Global:

The instance code is as follows:

 

Output: someting

The instance code is as follows:

$ GLOBALS []:

 

Output: Null

After understanding the above ideas, is there any dizzy situation?

The instance code is as follows:

 

The output is "3". The global variables $ a and $ B are declared in the function. all referenced variables of any variable point to the global variable.

Why isn't it 2? isn't it not affected outside the function? please note that $ B does not reference and modify in the function, but the modified $ B points to the physical memory value, therefore, the external input is 3.

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.