Scope and difference of variables in PHP Global and $ GLOBALS

Source: Internet
Author: User
Tags constant php file php code

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 with PHP Global variables.

PHP hack usage tips
Code implementation php gtk write 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 code is as follows: Copy code

$ A = 123;
 
Function aa ()
{
Global $;
// If $ a is not defined as a global variable
The function body cannot access $.
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 code is as follows: Copy code
$ Glpbal $;
$ A = 123;
Function f ()

Echo $ a; // error,
}

Let's take a look at the following example:

The code is as follows: Copy code


Function f ()

Global $ a; $ a = 123;
}  
F ();
Echo $;

// Correct, available

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, we declare global $ a. But there are a lot of functions and many variables that cannot be repeatedly declared? 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], $ x, so that we only need to declare a global $ x.

Answer3: I tried your method. No.

Answer4: modify your php. Ini file.

Set PHP Global variable to on


, Let's take a look at the complex points:

The code is as follows: Copy code

// A. php file

<? Php
Function Test_Global ()
{
Include 'B. Php ';
Test ();
}

$ A = 0;
Test_Global ();
Echo $;
?>

// B. php file

<? Php
Function Test ()
{
Global $ a; // declare that the $ a variable used in the Sum function is a global variable.
$ A = 1;
}
?>

Why is the output 0 ?!!

In a user-defined function, a local function range is 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. Rush out of the local function

The code is as follows: Copy code

// A. php file

<? Php
Function Test_Global ()
{
Test ();
}
Include 'B. Php'; // Remove include from the local Test_Global function
$ A = 0;
Test_Global ();
Echo $;
?>

// B. php file

<? Php
Function Test ()
{
Global $;
$ A = 1;
}
?>


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 example:

PHP code

 

The code is as follows: Copy code

<? Php

// Example 1

Function test_global (){

Global $ var1, $ var2;

$ Var2 = & $ var1;

 }

Function test_globals (){

$ GLOBALS ['var3'] = & $ GLOBALS ['var1'];

 }

$ Var1 = 5;

$ Var2 = $ var3 = 0;

Test_global ();

Print $ var2. "n ";

Test_globals ();

Print $ var3. "n ";

?>

The execution result is:

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.

$ GLOBALS reference to 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 it 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:

PHP code

 

The code is as follows: Copy code

<? Php

// Example 2

Function test (){

Global $;

Unset ($ );

 }

$ A = 1;

Test ();

Print $;

?>

The execution result is:

1

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

None of them. In fact, the unset function works. It is used to drop $ a from the test function to the unset function. You can add it to the test () function.

Print $;

To test!

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 actually called as an external variable, and the internal and external functions will always be consistent.

You can better understand the two columns below:

Global:

The code is as follows: Copy code

<? Php

Function myfunction (){

Global $ bar;

Unset ($ bar );

}

$ Bar = "someting ";

Myfunction ();

Echo $ bar;

?>

Output: someting

$ GLOBALS []:

<? Php

Function foo ()

{

Unset ($ GLOBALS ['bar']);

}

$ Bar = "something ";

Foo ();

Echo $ bar;

?>

Output: Null

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

The code is as follows: Copy code

<? Php

$ A = 1;

$ B = 2;

Function Sum ()

{

Global $ a, $ B;

$ B = $ a + $ B;

}

Sum ();

Echo $ B;

?>

The output will be "3 & Prime ;. 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 example:

PHP code

 

The code is as follows: Copy code

<? Php

// Example 1

Function test_global (){

Global $ var1, $ var2;

$ Var2 = & $ var1;

 } 

Function test_globals (){

$ GLOBALS ['var3'] = & $ GLOBALS ['var1'];

 } 

$ Var1 = 5;

$ Var2 = $ var3 = 0;

Test_global ();

Print $ var2. "n ";

Test_globals ();

Print $ var3. "n ";

?>

 

The execution result is:

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.

$ GLOBALS reference to 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 it 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:

PHP code

The code is as follows: Copy code

<? Php

// Example 2

Function test (){

Global $;

Unset ($ );

 } 

  

$ A = 1;

Test ();

Print $;

?>

 

The execution result is:

1

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

None of them. In fact, the unset function works. It is used to drop $ a from the test function to the unset function. You can add it to the test () function.

Print $;

To test!

 

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 actually called as an external variable, and the internal and external functions will always be consistent.

You can better understand the two columns below:

Global:

The code is as follows: Copy code

<? Php

Function myfunction (){

Global $ bar;

Unset ($ bar );

}

$ Bar = "someting ";

Myfunction ();

Echo $ bar;

?>

Output: someting

 

The code is as follows: Copy code

$ GLOBALS []:

<? Php

Function foo ()

{

Unset ($ GLOBALS ['bar']);

}

$ Bar = "something ";

Foo ();

Echo $ bar;

?>

Output: Null

 

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

The code is as follows: Copy code

<? Php

$ A = 1;

$ B = 2;

Function Sum ()

{

Global $ a, $ B;

$ B = $ a + $ B;

}

Sum ();

Echo $ B;

?>

The output will be "3 & Prime ;. 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.