Help - Search - Members - Calendar
Full Version: & before variables?
Invision Power Services > Community Forums > Community Web Design and Coding
_
I've seen in PHP sometimes programs are using a & in front of some variables. What difference does it do? wassat.gif

Example:
CODE
function myFunc(&$variable)
Michael K.
They're references. Manual. :-"
Rikki
It means that variable is passed by reference. When you do this:

CODE

function test($myvar)
{
$myvar = $myvar + 5;
return $myvar;
}

$variable = 2;

test($variable);


Then what is happening is that when the function is called, PHP copies the contents of $variable into a new variable called $myvar. So by doing the above, you now have 2 copies of the contents. You can't modify the *original* $variable in the function, only the copy, $myvar.

When passing by reference, instead of making a copy of the variable, you make a reference to the original.

One way to think of it is like shortcuts on Windows. You have one copy of the program file, but you can create shortcuts to it. The shortcut isn't the program itself, but a reference to the program.

See http://www.php.net/references for more info and examples. My explanation is only a simple description.
_
Alright, just to make sure:

CODE
$original = array(
'foo' => 'bar'
);
$reference =& $original;

unset($original['foo']);


Does this also make $reference['foo'] unset?

Also, what is the difference between:

CODE
$foo = $bar;


and:

CODE
$foo =& $bar;


blink.gif
Michael K.
QUOTE(Veracon @ Mar 10 2005, 07:29 AM)
Alright, just to make sure:

CODE
$original = array(
'foo' => 'bar'
);
$reference =& $original;

unset($original['foo']);


Does this also make $reference['foo'] unset?


It does.

QUOTE
Also, what is the difference between:

CODE
$foo = $bar;


and:

CODE
$foo =& $bar;


blink.gif
*

The one's a reference and the other isn't. Unless you meant the difference between "$foo =& $bar" and "$foo = &$bar", in which case, there is no difference.
Rikki
Yes, unsetting the original variable would make references useless.

OK, for the second part. Imagine this:

CODE

$bar = 5;

$foo = $bar;

$bar++;

echo $foo;


That would echo 5, because when you iterate $bar, it has nothing to do with $foo because they are two separate variables.

But if you did:

CODE

$bar = 5;

$foo =& $bar;

$bar++;

echo $foo;


That would echo 6, because $foo is a reference of $bar. That is, any changes you make to $bar are reflected in $foo because $foo simply points to $bar.
_
What would I do without IPS? biggrin.gif
Tseia
QUOTE(Veracon @ Mar 10 2005, 02:42 PM)
What would I do without IPS? biggrin.gif
*


I know I'd probably spend a lot less time online, but I've heard some people cry if they're forced away for a time...

I learnt about references the other day - quite nifty.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.