Difference between global and $ GLOBALS in php

Source: Internet
Author: User
Tags constant php code

According to the official explanation

1. $ GLOBALS ['var'] is an external global variable.

2. global $ var is an external reference or pointer with the same name as $ var.

Let's take a look at the following example:

For example:

 

The code is as follows: Copy code
<? Php
$ Var1 = 1;
$ Var2 = 2;
Function test (){
$ GLOBALS ['var2'] = & $ GLOBALS ['var1'];
    } 
Test ();
Echo $ var2;
?>

The normal print result is 1.

The code is as follows: Copy code


<? Php
$ Var1 = 1;
$ Var2 = 2;
Function test (){
Global $ var1, $ var2;
$ Var2 = & $ var1;
    } 
Test ();
Echo $ var2;
?>

 

The unexpected result is 2.

Why is the result 2 printed? In fact, it is because the $ var1 reference points to the reference address of $ var2. The actual value is not changed.


We all know that all the variables produced by functions in PHP are private variables of the function, so the variables produced by global 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

Function test (){
Global $;
Unset ($ );
}

$ A = 1;
Test ();
Print $;
?>

Performance:
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 participate in it after the function.
Print $;
That is to say, global generates the alias variable "$ a" for the external $ a of the test function, in order to be different from the external $
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 points to the physical memory address pointed to by var1.
So we come to the conclusion that the difference between global and $ GLOBALS [] in the function is:
Global generates an alias variable in the function that points 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 will occur, example 1.
$ GLOBALS [] the actual call is an external variable, and the internal and external functions will always be consistent!

You can compare the following two columns:

The code is as follows: Copy code
Global:
Function myfunction (){
Global $ bar;
Unset ($ bar );
}
$ Bar = "someting ";
Myfunction ();
Echo $ bar;
?>
Output: someting
$ Global []:
Function foo ()
{
Unset ($ GLOBALS ['bar']);
}
$ Bar = "something ";
Foo ();
Echo $ bar;
?>
Output: Null

Global variables in PHP are a little different from those in C language. In C language, global variables take effect in functions unless they are enveloped by local variables. This may cause some problems. Some may carelessly change a global variable. Global variables in PHP must be declared as global when used in functions.
Example of applying global

The code is as follows: Copy code

$ A = 1;
$ B = 2;

Function Sum ()
{
Global $ a, $ B;

$ B = $ a + $ B;
}

Sum ();
Echo $ B;
?>

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

Let's look at an example.

The code is as follows: Copy code

<? Php
$ Var1 = 1;
Function test (){
Unset ($ GLOBALS ['var1']);
    } 
Test ();
Echo $ var1;
?>

Because $ var1 is deleted, nothing is printed.

The code is as follows: Copy code

<? Php
$ Var1 = 1;
Function test (){
Global $ var1;
Unset ($ var1 );
    } 
Test ();
Echo $ var1;
?>

Accidentally printed 1. It indicates that only the alias is deleted. | the referenced value is not changed.

Global problem analysis:
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 define a constant in config. inc. php: define (constant name, constant value)
Then, in other places that need to be used, require 'config. inc. Php ',
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.

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.