59

What does a & in front of a variable name mean?

For example &$salary vs. $salary

Martijn
  • 15,791
  • 4
  • 36
  • 68
funk-shun
  • 4,331
  • 11
  • 32
  • 41

4 Answers4

86

It passes a reference to the variable so when any variable assigned the reference is edited, the original variable is changed. They are really useful when making functions which update an existing variable. Instead of hard coding which variable is updated, you can simply pass a reference to the function instead.

Example

<?php
    $number = 3;
    $pointer = &$number;  // Sets $pointer to a reference to $number
    echo $number."<br/>"; // Outputs  '3' and a line break
    $pointer = 24;        // Sets $number to 24
    echo $number;         // Outputs '24'
?>
Randy the Dev
  • 25,810
  • 6
  • 43
  • 54
  • 1
    Quick question, when and why should I be using this instead of just assigning a new value to the variable? Something like this: `$number = 3; echo $number; //Outputs 3 $number = 5; echo $number; //Outputs 5` – Brian Moreno Jun 14 '17 at 14:53
  • 1
    An example would be when you want to pass the reference into a function. This way, the original values are updated instead of the function's local variable! – parker_codes Mar 05 '18 at 23:56
3

It's a reference, much like in other languages such as C++. There's a section in the documentation about it.

rmeador
  • 25,504
  • 18
  • 62
  • 103
1

To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable). For instance, the following code snippet outputs 'My name is Bob' twice:

$foo = 'Bob';              // Assign the value 'Bob' to $foo
$bar = &$foo;              // Reference $foo via $bar.
$bar = "My name is $bar";  // Alter $bar...
echo $bar;
echo $foo;                 // $foo is altered too.

One important thing to note is that only named variables may be assigned by reference.

$foo = 25;
$bar = &$foo;      // This is a valid assignment.
$bar = &(24 * 7);  // Invalid; references an unnamed expression.

function test()
{
   return 25;
}

$bar = &test();    // Invalid.

Finally, While getting a reference as a parameter, It must pass a reference of a variable, not anything else. Though it's not a big problem because we know why we are using it. But It can happen many times. Here's how it works:

function change ( &$ref ) {
  $ref = 'changed';
}

$name = 'Wanda';

change ( $name ); // $name reference
change ( 'Wanda' ); // Error

echo $name; // output: changed

README (for more information)

Tahazzot
  • 1,224
  • 6
  • 27
-1

It is used to pass a variable by reference.

Read more in the PHP documentation.

Lucan
  • 2,907
  • 2
  • 16
  • 30
Sam Day
  • 1,713
  • 9
  • 17
  • 2
    only **objects**? may be the more appropriate word is "variable"? – zerkms Sep 22 '10 at 22:40
  • 1
    Objects are passed by reference by default in PHP5 anyway, so there's no need for the `&` when dealing with objects. @user, It gets the parameter `foo` from the object assigned to the variable `$a` – Randy the Dev Sep 22 '10 at 22:48