What is the difference between global and $ globals in PHP? Paste the code first. this is global.
After testglobal is called, assign a value to $ var1 and then print the values of $ var1 and $ var2.
The output result is
5---5 second output: 20---10
Assign the address of var1 to var2 in the function (I tested that changing the value of var1 in the function body will also change the value of var2), but assign a value to var2 after the call, the value of var1 is not changed. modifying the value of var1 after global will affect the value of var1 in vitro of the function.
This is $ globals code
Using $ globals changes the values of var1 and var2 both in the function body and in vitro.
Problem:
1: Does a global reference variable belong to a pass-through reference. after referencing a variable, it actually only transmits the value of the variable to the function body? However, changing the value of var1 in global affects the value of var1 in vitro. why?
2: What are the differences between global and $ globals?
I am looking forward to getting some advice. the format of the first question is a bit messy and I will improve it.
Reply to discussion (solution)
Don't worry about this problem? The manual already has a clear description
Global $ var1, $ var2; // $ var1 and $ var2 are references to global variables.
$ Var2 = & $ var1; // assign $ var2 a value to $ var1. This removes $ var2's reference to the global variable $ var2.
Don't worry about this problem? The manual already has a clear description
Global $ var1, $ var2; // $ var1 and $ var2 are references to global variables.
$ Var2 = & $ var1; // assign $ var2 a value to $ var1. This removes $ var2's reference to the global variable $ var2.
Thank you. please take a look at the next Manual.