Good afternoon everyone, I saw this knowledge point on the Internet:
global variables declared inside a 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 a reference to the external variable $var1, unset($GLOBALS['var1']); causes the external $var1 to disconnect from the memory (the variable $var1 is destroyed)
Then here comes the question
According to the theory at the beginning of the question, even if the external $var1 is unset, the $var1 inside the function can still be accessed from outside the function? ($var1 in the function is also global!), but why does echo $var1 report an error in the end?
Please give me some advice, thank you!
In addition, I have another question. I hope someone can help me.
https://segmentfault.com/q/10...
Good afternoon everyone, I saw this knowledge point on the Internet:
global variables declared inside a 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 a reference to the external variable $var1, unset($GLOBALS['var1']); causes the external $var1 to disconnect from the memory (the variable $var1 is destroyed)
Then here comes the question
According to the theory at the beginning of the question, even if the external $var1 is unset, the $var1 inside the function can still be accessed from outside the function? ($var1 in the function is also global!), but why does echo $var1 report an error in the end?
Please give me some advice, thank you!
In addition, I have another question. I hope someone can help me.
https://segmentfault.com/q/10...
It can be understood like this:global $var1;
is equal to$var1=&$GLOBALS['var1'];
You can compare the running results of the upper and lower sections
Let me add a paragraph too
What you declare is a global variable. Because it is global, you can delete it inside or outside the function.
After deletion, it no longer exists whether you are inside or outside the function.
Note:
The variables inside and outside the function are the same and point to the same pointer.
After declaring a global variable, it does not create a variable inside the function and a variable outside the function.
Addition:
My understanding is wrong, what @Mi Mo
downstairs said:
global $var1;等于$var1=&$GLOBALS['var1'];
is correct.
Just to add:
I just realized that I didn’t see it clearly before:
global $var1;等于$var1=&$GLOBALS['var1'];
This sentence is correct, but I didn’t notice the existence of & before.
Because it seems easier to understand if you remove the &.
But in fact & exists, so it’s still the same as what I said above: $var1 inside and outside points to the same address.
Let’s look back at the example:
$var1 = 1; function test(){ global $var1; unset($GLOBALS['var1']); echo $var1; } test(); //1 已经删除了$var1,为什么函数内的$var1还存在呢? echo $var1;//Undefined
-->Question: Since they are the same thing, why does one have output and the other report an error?
Try another one:
$var1 = 1;
function test(){
global $var1; $GLOBALS['var1']=99; echo $var1;
}
test(); //99
echo $var1;//99
-->If you change one, the other will also change at the same time, which means they should still be the same thing, right?
So, what’s the problem?
In fact, the problem lies in the unset() function:
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the variable contents are destroyed.
(Reference: http://blog.csdn.net/ebw123/a...)
I have found a preliminary clue now, look at the code below:
Example 1
Example 2
Example 3
Based on the question and the code in this reply, the summary is as follows
Use unset($GLOBALS['var']) within the function;
1: It will destroy the $var variable outside the function (because $GLOBALS[ 'var'] is the outer $var itself)
2:
If there is a global variable (can be accessed externally) before unset($GLOBALS['var']); inside the function, then unset($GLOBALS['var']); will cancel the external access to the global variable in the function "right"
Inside the function, if there is already a global variable (can be accessed externally) after unset($GLOBALS['var']);, then unset($GLOBALS['var']); will not interfere with external access inside the function "Rights" of global variables
Question:
In addition to unset($GLOBALS['var']); you can destroy the external variable $var to reduce the refcount of the zval it points to by one,
Could it also change the scope of global variables originally within a function from global to local (causing global variables inside the function to be inaccessible from the outside)?
Hope, God can give me some guidance.