I think of global variables when I am idle, so I decided to Google Baidu, And the search results were quite satisfactory. I just clicked on the results to find out, I wiped it, is there such a bad guy? It's just the same as his mother, and even the wrong words are the same.
I just want to vomit, not to think of the spam Nima. A mouthful of mouth... Mom, only drool, no grass
To eliminate the solution, you can test it by yourself! ------ This is my little tail. Have you seen it? What? No small tail... Emma, what about my little tail!
Sample Code:
Test 1:
1 <?php2 function test(){3 global $var;4 $var ++;5 }6 $var = 1;7 test(); 8 echo $var;
Result: 2
Well, it's exactly what you understand.
Test 2:
1 <?php2 global $var;3 $var = 1; 4 function test(){ 5 $var ++; 6 } 7 test(); 8 echo $var;
Result: I think you all know that it is the result of Shenma.
Summary: global variables defined in the function body can be used in vitro, and global variables defined in vitro cannot be used in the function body;
Again:
Test 3:
1 <?php 2 $var = 1; 3 function test(){ 4 global $var; 5 unset($var); 6 } 7 test(); 8 echo $var;
Hey hey, hey, you think it's time to output the magic horse. I wiped it and input 1. The unset of TM is invalid. Why? Well, I will explain to you that the global $ Val in the function, but there is also a $ Val outside the function. You must absolutely believe that it is not him,
He is an alias for him. The name is recognized by Shenma. Do you want to ask me? Hey!
In Test 1, $ var ++ returns 2, and test 3 unset ($ Val). Why is it still 1? Because he operates on the alias, we will describe it here, alias him => it. You have killed it, but there is another one. Well, we can explain it very clearly. Of course, I should have a little tail! Yeah... What about my little tail!
OK. Now that we have talked about global, let's also talk about this $ globals []. Here, if you want to say that this is not the same person, one is a small name, one is still a small name; then I will be hey, oh no, it is hehe;
Official explanation: $ globals ['var'] is the external global variable itself, and global $ VaR is the reference or pointer of the external $ var with the same name.
Check the test;
Test 4:
1 <?php 2 $var = 1; 3 function test(){ 4 unset($GLOBALS[‘var‘]); 5 } 6 test(); 7 echo $var;
Result: Nothing. Why is it different from Test 3? Who told you that she (he/she) is the same. He is the only one here, without it;
Understand !?
Therefore, according to the official and test cases, global $ VaR is actually $ Var = & $ globals ['var']. (Don't Ask Me & what is it !)