Why Do Some PHP Variables Have an Ampersand Before Them?

If you’ve worked with PHP a bit, you may have seen someone put an ampersand (“&”) in front of a variable, like this:

add_action('admin_print_scripts', array(&$this,'admin_scripts'));

In this example, we’re using a WordPress hook, and passing an array with a class and method combination, instead of just a function name. (The $this variable, of course, means that the function name is inside the same class.) But what does the ampersand before the variable mean, though? To tell you the truth, I didn’t know until recently…

When you prefix a variable with an ampersand, you’re creating a “reference.” PHP references are like shortcuts or symlinks on your computer. You can create a pointer variable that is just another name for the same data.

$variable = 'Lorem ipsum';
$new = &$variable;
$variable = 'Some new data';
echo $new; //Prints "Some new data"

Pretty nifty. I’m sure you can think of some uses for that.

In the WordPress hook example, what I gather to be happening is that the plugin author was passing a reference to the $this variable, rather than the variable itself. I assume there is some sort of RAM/CPU-saving benefit to this.

  • http://www.problogdesign.com/ Michael Martin

    Ah, that’s pretty cool Matt. Would be interesting to see if there really was a performance benefit and what it would be, like you suggested! :)

    • http://twitter.com/michalkozak Michal Kozak

      Of course there is a performance benefit of doing so :). Not only that, it’s also less coding.

      First of all when you do something like:

      $b = &$a;

      You avoid arduous copying process which speeds up the whole program.

      Another example is when you’re using “foreach” loop for working with arrays. If you apply some changes inside foreach loop to array’s values, the original array will not be modified, since what you have inside foreach loop is just the copy – so you’ll modify just the copy, not the original.

      When you use & before particular array’s value to refference to the original – thus any change to the copy will also affect the original:

      $myArr = array (0 => “before change”);
      foreach($myArr as $id => &$element) {
      $element[$id] = “after change”;

      When you print $myArr you’ll see “after change”. If you would you & before $element in the loop, like this:

      foreach($myArr as $element)

      You’d see the “before change” when you’d print the $myArr.
      Hope I epxlained it well :)

    • http://twitter.com/michalkozak Michal Kozak

      SORRY, I made a mistake there. This is correct:

      $myArr = array (0 => “before change”);
      foreach($myArr as &$element) {
      $element = “after change”;

  • http://spanspek.org David Rodger

    >passing an array with a class and method combination,
    >instead of just a function name

    Well, an object reference and a function name.

    In this case (and not the examples which follow), it’s because Matt Mullenweg insists on maintaining compatibility with PHP4, despite the fact that support for PHP4 ended years ago.

  • Nikos

    Hi ,
    This functionality is depreciated from PHP5.2 .
    If you tray to run PEAR packages on PHP5.3 you get a lot of error messages that values by reference are depreciated .