9

Possible Duplicate:
Reference - What does this symbol mean in PHP?

what is the meaning of &$variable
and meaning of functions like

function &SelectLimit( $sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0 ) { $rs =& $this->do_query( $sql, $offset, $nrows, $inputarr); return $rs; } 
1

2 Answers 2

10

Passing an argument like so: myFunc(&$var); means that the variable is passed by reference (and not by value). So any modifications made to the variable in the function modify the variable where the call is made.

Putting & before the function name means "return by reference". This is a bit very counter-intuitive. I would avoid using it if possible. What does it mean to start a PHP function with an ampersand?

Be careful not to confuse it with the &= or & operator, which is completely different.

Quick test for passing by reference:

<?php class myClass { public $var; } function incrementVar($a) { $a++; } function incrementVarRef(&$a) { // not deprecated $a++; } function incrementObj($obj) { $obj->var++; } $c = new myClass(); $c->var = 1; $a = 1; incrementVar($a); echo "test1 $a\n"; $a = 1; incrementVar(&$a); echo "test2 $a\n"; // deprecated $a = 1; incrementVarRef($a); echo "test3 $a\n"; incrementObj($c); echo "test4 $c->var\n";// notice that objects are // always passed by reference 

Output:

Deprecated: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of incrementVar(). [...] test1 1 test2 2 test3 2 test4 2 
Sign up to request clarification or add additional context in comments.

3 Comments

Returning references is not deprecated, but returning object references are in some cases.
Perhaps also worth mentioning that passing by reference was widely used in php4 and is almost never a good idea in php5.
Awesome explanation.
2

The ampersand - "&" - is used to designate the address of a variable, instead of it's value. We call this "pass by reference".

So, "&$variable" is the reference to the variable, not it's value. And "function &func(..." tells the function to return the reference of the return variable, instead of a copy of the variable.

See also:

4 Comments

Calling it address, is a bit misleading. It is a reference to the variable. An address is something that would refer to a position in memory, e.g. a pointer. This is distinctly not a pointer.
@troelskn: I agree, and mostly updated my answer to reflect this. However, I've noticed that, for people who have never heard of pass by reference or pass by value, they can understand it easier by calling it an "address". Although this may not technically be correct, it can be a beneficial corner to cut for noobs.
P.S. in many languages, pass by reference is accomplished by returning a pointer (if you trace it back to the underlying C or Assembly code).
Yes, conceptually it works very much like a pointer, so I guess it's OK to use as a metaphor. I just think it's important to make the point that it is not a pointer, since there are some important differences. Notably, references offers no performance benefit (on the contrary).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.