10

Please see this code:

function addCounter(&$userInfoArray) {
    $userInfoArray['counter']++;
    return $userInfoArray['counter'];
}

$userInfoArray = array('id' => 'foo', 'name' => 'fooName', 'counter' => 10);
$nowCounter = addCounter($userInfoArray);

echo($userInfoArray['counter']);

This will show 11.

But! If you remove "&"operator in the function parameter, the result will be 10.

What's going on?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Deckard
  • 1,309
  • 5
  • 27
  • 55
  • 2 perfect answers for only 3 mins! Are you guys all genious or something? or expecting me to ask this question. So surprised! – Deckard Oct 18 '10 at 08:44
  • *(related)* [What does this symbol mean in PHP](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Oct 18 '10 at 08:44
  • Oh...I tried to google it, but failed... So embarrasing.. – Deckard Oct 18 '10 at 08:49

4 Answers4

40

The & operator tells PHP not to copy the array when passing it to the function. Instead, a reference to the array is passed into the function, thus the function modifies the original array instead of a copy.

Just look at this minimal example:

<?php
function foo($a) { $a++; }
function bar(&$a) { $a++; }

$x = 1;
foo($x);
echo "$x\n";    
bar($x);
echo "$x\n";
?>

Here, the output is:

1
2

– the call to foo didn’t modify $x. The call to bar, on the other hand, did.

Konrad Rudolph
  • 482,603
  • 120
  • 884
  • 1,141
6

Here the & character means that the variable is passed by reference, instead of by value. The difference between the two is that if you pass by reference, any changes made to the variable are made to the original also.

function do_a_thing_v ($a) {
    $a = $a + 1;
}
$x = 5;
do_a_thing_v($x);
echo $x; // echoes 5

function do_a_thing_r (&$a) {
    $a = $a + 1;
}
$x = 5;
do_a_thing_v($x);
echo $x; // echoes 6
Hammerite
  • 19,804
  • 4
  • 65
  • 82
2

When using the ampersand prior to a variable in a function call, it associates with the original variable itself. With that, the code you posted is saying that it will add 1 to the counter of the original array. Without the ampersand, it takes a copy of the data and adds to it, then returns the new counter of 11. The old array still remains intact at 10 and the new counter variable returned turns into 11.

http://www.phpreferencebook.com/samples/php-pass-by-reference/

is a good example.

pyr0
  • 21
  • 1
2

Maybe I can add to the other answers that, if it is an object, then it is not "the object passed as value", but it is "the object's reference is passed as a value" (although I am asking what the difference is between "the object is passed by reference" vs "the object's reference is passed by value" in the comments). An array is passed by value by default.

Information: Objects and references

Example:

class Foo {
    public $a = 10;
}

function add($obj) {
    $obj->a++;
}

$foo = new Foo();
echo $foo->a, "\n";

add($foo);
echo $foo->a, "\n";

Result:

$ php try.php
10
11
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
nonopolarity
  • 130,775
  • 117
  • 415
  • 675
  • Thank you another Mr.Genious. – Deckard Oct 18 '10 at 08:59
  • 1
    This is a common mistake. Objects are *not* passed by reference. Rather, their reference is passed by value. This is a crucial difference! Try modifying the reference itself, rather than an instance member (i.e. write `$obj = new Foo()` inside the method). Ironically, the page you linked even *says* that your quote isn’t true! So you must have known that you wrote something false. – Konrad Rudolph Oct 18 '10 at 09:00
  • if it is passed by reference, vs if its reference is passed by value, if `$obj = new Foo()` inside the method, are the results just the same? i.e. when print out one more time, it is still `11`. – nonopolarity Oct 18 '10 at 09:09
  • since it is a separate item, how about discussing in http://stackoverflow.com/questions/3957801/is-there-simple-php-code-to-distinguish-passing-the-object-as-reference-vs-pas – nonopolarity Oct 18 '10 at 09:34