简体   繁体   中英

PHP Globals and Reference Difference Confusion

Could some one please explain to me what the difference in the following is:

$var = 'something';
function myFunc(){
    global $var;
    // do something
}

and (note: the reference sign &)

$var = 'something';
function myFunc(&$var){
    // do something
}

I can't understand the difference between these two methods.

Update

Hmmm... I think i'm getting alittle confused here:)

$var = 1;
$var2 = 2;
function myFunction(){
    global $var, $var2;
    $var = $var + $var2;
}

// echo $var would return 3 here.


$var = 1;
$var2 = 2;
function myFunction(&$a, &$b){
    $a = $a + $b;
}

// echo $var would return 3 here aswell.


I understand those part already, But what i don't understand is the difference between these two approaches, am i wrong to assume that these two approaches are technically the same thing, with different concept of how they are wrote by the coder and also how they are handled by PHP?

If i'm wrong to assume this, then it would really help if you could provide better examples, maybe something that can be accomplished using only one of the above approaches?

ok, i noticed one difference while writing This Update , which is that when doing references i can chose new variable names while keeping the pointer to the variable in the functions scope.

In the second code block, the external $var and the internal $var are the same element. On the other hand, the third code block behaves a lot more like the first -- but you can access and modify the variable from the sending function; in the first code block, any edits you make to the variable internally are not available outside of the scope of the function.


EDIT to match the edits to the question.

The & means we are passing the variable by reference. The best way to describe it is by example.

function test2(&$var) { $var = 2; }
function test() {
   $abc = 1;
   test2($abc);
   echo $abc;
}
test();

This code will print a 2 to the screen.

$var = 0;
function test()
{
   global $var;
   $var = 1;
}
test();
echo $var;

This will print 1;

In the first example, the local $var variable inside myFunc() will be assigned by reference to the variable with the same name that exists in the global scope (accessible through the superglobal $GLOBALS ). PHP translates that to something like:

$var = 'something';
$GLOBALS['var'] = &$var;
function myFunc(){
    $var = &$GLOBALS['var'];
    // do something
}

In the second example, you pass the variable explicitly to the function, thus it is not taken from the global scope.

$var = 'something';
$GLOBALS['var'] = &$var;
function myFunc(&$var){
    // do something
}
myFunc($var);

Both examples will reference the exact same variable, but using different mechanisms.

There is no technical difference at all between your two examples; they will always behave in an identical manner.

That leaves us looking for other types of differences, and there is a pretty significant code quality difference here: In the first version, looking at the call site will not tell you that the function depends on the global. In the second one it will (it's being passed in as a parameter). So the second form is, in my book at least, superior.

In addition, another difference (although one that does not apply to this example) is that the first form can only be used if your variable is in the global scope to begin with; the second will work everywhere.

You should also take into account that both these methods have an in/out argument. Normally you would not be writing code like this if it were just one argument (since you can return the out value instead) so it's interesting to consider the case where more than one arguments are being given. This is a pretty rare, and it can be argued that it's better to just return an array with multiple values rather than take multiple arguments by reference to make the code even easier to follow.

So in conclusion:

  • They do the same thing
  • If you are going to use one of them, prefer the second form (clearer, more flexible)
  • Consider using none of them and doing things otherwise if it doesn't require writing a substantial amount of additional code

One passes by value and one passes by reference:

By value:

$var = 'something';
function myFunc($var){
    $var = 'hello';
}
echo $var; //something

By reference:

$var = 'something';
function myFunc(&$var){
    $var = 'hello';
}
echo $var; //hello

For more information, the PHP manual has a page on passing by reference: http://php.net/manual/en/language.references.pass.php

In the case of globals, you are essentially pulling a variable from the global scope and using it inside of the function. It's like implicitly passing by reference.

This page, in particular the global section explains: http://us.php.net/manual/en/language.variables.scope.php

Please note that globals should typically be avoided. The short version of why is because they make knowing the state of a program difficult, and later changes can cause unexpected side effects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM