8

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;
} 
Community
  • 1
  • 1
Mostafa Darwish
  • 273
  • 1
  • 4
  • 14

2 Answers2

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
Community
  • 1
  • 1
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236
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:

Community
  • 1
  • 1
cegfault
  • 6,442
  • 3
  • 27
  • 49
  • 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 Oct 21 '12 at 21:22
  • @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. – cegfault Oct 21 '12 at 21:32
  • 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). – cegfault Oct 21 '12 at 21:33
  • 1
    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). – troelskn Oct 22 '12 at 07:07