On the Internet saw that the difference written only in writing, is it?
from the bottom of his friend Saidolim
wrote one example, I want to continue the question …
there is such a code
/// Example 1
$ STR = "SIMPLE TEXT";
$ A = FUNCTION ()
{
Global $ str;
$ str = "edited";
};
$ a ();
Echo $ str. "\ n"; /// edited
/// Example 2.
$ STR = "SIMPLE TEXT";
$ A = FUNCTION ()
{
$ Globals ["Str"] = "edited";
};
$ a ();
Echo $ str. "\ n"; /// edited
/// Example 3.
$ STR = "SIMPLE TEXT";
$ temp = "";
$ A = FUNCTION ()
{
$ Globals ["Temp"] = & amp; $ globals ["str"];
};
$ a ();
Echo "[". $ str. "] [". $ temp. "] \ n"; /// [SIMPLE TEXT] [Simple Text]
/// Example 4.
$ STR = "SIMPLE TEXT";
$ temp = "";
$ A = FUNCTION ()
{
Global $ str, $ temp;
$ temp = & amp; $ str;
};
$ a ();
Echo "[". $ str. "] [". $ temp. "] \ n"; /// [SIMPLE TEXT] []
The first example displays as expected modified value, the second example is the same … It would seem difference no, go further! In the third example, in the function, we make a tough reference for the elements of a superglobal array displays as expected one and the same word, and now we look at the fourth example and … I don’t line in a stupor and displays only the variable $ str
, and the variable $ Temp
leaves empty, why?
p.s wrote here because I did not want to spam questions on the same
Answer 1, Authority 100%
Instructions Global
addresses the Global array $ globals
. And gets the value of the variable $ globals ['var']
. The difference is only in writing.
p.s. Tip when using global variables: stay away from them, using them only when you can’t do without them.
Abuse of global variables can bring a person analyzing your code to white hot.
Answer 2, Authority 50%
Let’s turn to the documentation (this is, by the way, it is very useful to do)
$ globals – links to all variables of the global area of visibility
This ‘superglobal’ or automatic global variable. it
Simply means that it is available in all contexts of the script. No
need to perform Global $ Variable; To access her inside
method or functions.
In fact, they provide access to the same objects. Difference only in the method of use.
And how correctly noted @firepro, the use of global
either $ globals
is very bad.
Answer 3, Authority 50%
Global
I think it works faster. Since the approach is different. But when you use a variable, it is difficult for you to understand it global or not.$ globals
more readable and easy to understand. Since it is obvious that the global variable.- if you want to use
unset
global variable, you must useunset ($ globals ['varname'])
, and notglobal $ varname; Unset ($ Varname);
.
I think more detail. You can learn here
UPD
To compare these commands. I propose to check the following code:
function test_global () {
Global $ var1, $ var2;
$ var2 = & amp; $ var1;
}
Function Test_Globals () {
$ Globals ['Var3'] = & amp; $ Globals ['Var1'];
}
$ var1 = 5;
$ var2 = 0;
$ var3 = 0;
test_global ();
Print $ var1. "& GT;". $ var2. "";
Echo "& lt; br / & gt;";
test_globals ();
Print $ var1. "& GT;". $ var3;
result
5 & gt; 0.
5 & gt; 5
But I would advise you to use the following scheme when you work with variables:
function MyFunc (& amp; $ MyVar)
{
$ MyVar = 10;
}
$ foo = 0;
myFunc ($ foo);
var_dump ($ foo); // result 10
Here, as you can see, the variable is passed to the function as a reference, and the function has the right to change its value. And Inside the function it is easier to understand that someone gave you a variable and you change its value.
It is better to bypass globality than to use it.
Answer 4, authority 25%
There is actually a difference between the $ GLOBALS superglobal variable and the global keyword in php.
The point is that $ GLOBALS refers to a variable directly, while global injects a variable from the global scope (and, as far as I can tell , only from it) to the corresponding local scope.
The documentation is about this, but only in passing … The situation is similar for Zend Engine 2 and phpng.