About global in PHP

Source: Internet
Author: User
Dear friends, good noon, I saw such a point on the Internet: the global variable declared in the function can be accessed by the external main program, and then I ran the following code, the above conclusion is also verified: {code ...} based on the above theory, I wrote the following code: {code ...} test... dear friends, good noon, I saw such a knowledge point on the Internet:
The global variable declared in the function can be accessed by the external main program.

Then I ran the following code and verified the above conclusion:


  

Based on the above theory, I wrote the following code:


  

Global $ var1 in the test function is an external variable $ var1 reference, unset ($ GLOBALS ['var1']); disconnect External $ var1 from memory (the variable $ var1 is destroyed)

So the question is
According to the theory at the beginning of the question, even if the external $ var1 is unset, can the external function still access $ var1 inside the function? ($ Var1 in the function is also global !), But why does echo $ var1 report an error?
Thank you!

In addition, I have another question. I hope the gods can help me check it out.
Https://segmentfault.com/q/10...

Reply content:

Dear friends, good noon, I saw such a knowledge point on the Internet:
The global variable declared in the function can be accessed by the external main program.

Then I ran the following code and verified the above conclusion:


  

Based on the above theory, I wrote the following code:


  

Global $ var1 in the test function is an external variable $ var1 reference, unset ($ GLOBALS ['var1']); disconnect External $ var1 from memory (the variable $ var1 is destroyed)

So the question is
According to the theory at the beginning of the question, even if the external $ var1 is unset, can the external function still access $ var1 inside the function? ($ Var1 in the function is also global !), But why does echo $ var1 report an error?
Thank you!

In addition, I have another question. I hope the gods can help me check it out.
Https://segmentfault.com/q/10...

It can be understood as follows:global $var1;Equal$var1=&$GLOBALS['var1'];


  

Compare the running results of the upper and lower sections


  

Let me add one.


  

You declare a global variable because it is global, so you can delete it within or outside the function.
After deletion, no matter whether you are inside or outside the function, it does not exist.

Note:
All the variables inside and outside the function point to the same pointer.

After a global variable is declared, it does not create a variable either inside or outside the function.

Supplement:
My understanding is incorrect. @ mi downstairs
Said:

Global $ var1; equal to $ var1 = & $ GLOBALS ['var1'];

Is correct.

I would like to add:
I did not see it clearly before:

Global $ var1; equal to $ var1 = & $ GLOBALS ['var1'];

This sentence is correct, but I did not notice the existence.
It seems that removing & is easy to understand.
But in fact it exists, so it is still the same as what I mentioned above: $ var1 inside and outside points to the same address.

Let's look at the example again:

$ Var1 = 1; function test () {global $ var1; unset ($ GLOBALS ['var1']); echo $ var1 ;}test (); // 1 has deleted $ var1. Why does $ var1 still exist in the function? Echo $ var1; // Undefined

--> Question: Since it is the same thing, why is there an output and an error?

Try another one:
$ Var1 = 1;
Function test (){

global $var1;                       $GLOBALS['var1']=99;echo $var1;

}
Test (); // 99
Echo $ var1; // 99

--> Changing one and the other at the same time means they should be the same thing, right?

So where is the problem?
In fact, the problem lies in the unset () function:

When you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed.

(Reference: http://blog.csdn.net/ebw123/a ...)

Now I have found a clue and read the following code:

Example 1


   

Example 2


   

Example 3

    
   

Combine the code in the question and the content in this reply to summarize as follows:
Use unset ($ GLOBALS ['var']) in the function;

1: it will destroy the $ var variable outside the function (because $ GLOBALS ['var'] is the external $ var itself)

2:

  • If a global variable (which can be accessed externally) exists in the unset ($ GLOBALS ['var']) function, unset ($ GLOBALS ['var']); cancels the "right" of the global variable in the external access function"

  • If a global variable (which can be accessed externally) exists in the unset ($ GLOBALS ['var']) function, unset ($ GLOBALS ['var']); it will not interfere with the "right" of the global variable in the external access function"

Question:
Except for unset ($ GLOBALS ['var']); you can destroy the external variable $ var, so that the number of refcount values pointed to by zval is reduced by one,

Can it also change the scope of the global variable originally in the function from global to local (resulting in external access to the global variable in the function )?

Hope.

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.