Good afternoon, dear God, I saw such a point of knowledge on the Internet:
the global variable declared inside the function can be accessed by the external main program
Then I ran the following code, and also verified the above conclusion:
Based on the above theory, I have also written the following code:
The global $var 1 within the test function is a reference to the external variable $var1, unset ($GLOBALS [' var1 '), and the external $var1 is disconnected from the memory (the variable $var1 is destroyed)
So here's the problem.
According to the theory at the beginning of the question, even if the external $var1 is unset, the outside of the function can still access the $var1 inside the function. (The $var1 inside the function is also global!) ), but why is the last echo $var 1 error?
Also please the great god enlighten, thank you!
In addition, I have a question, I hope the great God can help me see
HTTPS://SEGMENTFAULT.COM/Q/10 ...
Reply content:
Good afternoon, dear God, I saw such a point of knowledge on the Internet:
The global variable declared inside the function can be accessed by the external main program
Then I ran the following code, and also verified the above conclusion:
Based on the above theory, I have also written the following code:
The global $var 1 within the test function is a reference to the external variable $var1, unset ($GLOBALS [' var1 '), and the external $var1 is disconnected from the memory (the variable $var1 is destroyed)
So here's the problem.
According to the theory at the beginning of the question, even if the external $var1 is unset, the outside of the function can still access the $var1 inside the function. (The $var1 inside the function is also global!) ), but why is the last echo $var 1 error?
Also please the great god enlighten, thank you!
In addition, I have a question, I hope the great God can help me see
HTTPS://SEGMENTFAULT.COM/Q/10 ...
As you can understand: global $var1; equals$var1=&$GLOBALS['var1'];
can compare upper and lower running results
I'm going to add some.
You declare a global variable, because it is global, so you can delete it inside the function or outside of the function.
After you delete it, it doesn't exist either inside or outside of the function.
Attention:
Both inside and outside the function are the same variable, pointing to the same pointer.
Once a global variable is declared, it does not create a variable within the function and outside of the function.
Add:
My understanding is wrong, downstairs @ mi moth
To say:
global $var1;等于$var1=&$GLOBALS['var1'];
is correct.
Add:
Before I found myself not to see clearly:
global $var1;等于$var1=&$GLOBALS['var1'];
There's nothing wrong with that, but I didn't notice the existence of & before.
Because it seems better to get rid of &.
But the fact that & is there, so, is still the same as I said: The $var1 is pointing at 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 it is the same thing, why does one have output, an error?
Try a different one:
$var 1 = 1;
function Test () {
global $var1; $GLOBALS['var1']=99;echo $var1;
}
Test (); 99
Echo $var 1;//99
-Change one, the other changes at the same time, which means they should be the same thing?
So, where is the problem?
In fact, the problem is in unset () This 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 contents of the variable are destroyed.
(Reference: http://blog.csdn.net/ebw123/a ... )
I found a little clue now, look at the following code:
Example 1
Example 2
Example 3
Combine the questions and the code in this reply to summarize the following
Use unset ($GLOBALS [' var '] within the function);
1: It will be destroyed by the $var variable outside the function (because $globals[' var ') is the external $var itself)
2:
Inside the function if the unset ($GLOBALS [' var ']); Before there is a global variable (which can be accessed externally), then unset ($GLOBALS [' var ']) will cancel the "right" of the global variable within the external access function.
Inside the function if the unset ($GLOBALS [' var ']); After the global variable (which can be accessed externally), the unset ($GLOBALS [' var ']) will not interfere with the "right" of the global variable within the external access function.
Questions:
Except for unset ($GLOBALS [' var ']); The external variable $var can be destroyed so that the number of refcount that it points to zval is reduced by one,
Can it also make the scope of the global variable within the function be changed from global to local (resulting in external unreachable function internal global variables)?
Hope, the great God pointing the maze.