6

I have seen in my journey to creaitng and building some of my php applications, the & symbol within front of vars, = and class names.

I understand that these are PHP References, but the docs i have seen and looked at seem to just not explain it in a way that i understand or confusing. How can you explain the following examples that i have seen to make them more understandable.

  public static function &function_name(){...}

  $varname =& functioncall();

  function ($var, &$var2, $var3){...}

Much appreciated

NikiC
  • 100,734
  • 37
  • 191
  • 225
Simon Davies
  • 3,668
  • 9
  • 41
  • 69
  • See it http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php `=& References` block. – Winston Feb 27 '13 at 18:23

2 Answers2

4

Let's say you have two functions

$a = 5;
function withReference(&$a) {
    $a++;
}
function withoutReference($a) {
    $a++;
}

withoutReference($a);
// $a is still 5, since your function had a local copy of $a
var_dump($a);
withReference($a);
// $a is now 6, you changed $a outside of function scope
var_dump($a);

So, passing argument by reference allows function to modify it outside of the function scope.

Now second example.

You have a function which returns a reference

class References {
    public $a = 5;
    public function &getA() {
        return $this->a;
    }
}

$references = new References;
// let's do regular assignment
$a = $references->getA();
$a++;
// you get 5, $a++ had no effect on $a from the class
var_dump($references->getA());

// now let's do reference assignment
$a = &$references->getA();
$a++;
// $a is the same as $reference->a, so now you will get 6
var_dump($references->getA());

// a little bit different
$references->a++;
// since $a is the same as $reference->a, you will get 7
var_dump($a);
Marko D
  • 7,576
  • 2
  • 26
  • 38
  • 1
    @ Marko D many thank for the above, i wanted to understand so if it was any use for any current projects and if not a mind though for future ones. – Simon Davies Feb 27 '13 at 21:37
0

Reference functions

$name = 'alfa';
$address = 'street';
//declaring the function with the $ tells PHP that the function will
//return the reference to the value, and not the value itself
function &function_name($what){
//we need to access some previous declared variables
GLOBAL $name,$address;//or at function declaration (use) keyword
    if ($what == 'name')
        return $name;
    else
        return $address;
}
//now we "link" the $search variable and the $name one with the same value
$search =& function_name('name');
//we can use the result as value, not as reference too
$other_search = function_name('name');
//any change on this reference will affect the "$name" too
$search = 'new_name';
var_dump($search,$name,$other_search);
//will output string 'new_name' (length=8)string 'new_name' (length=8)string 'alfa' (length=4)

Usually you use the method with Objects that implemented the same interface, and you want to choose the object you want to work with next.

Passing by reference:

function ($var, &$var2, $var3){...}

I'm sure you saw the examples, so I'll just explain how and when to use it. The basic scenario is when do you have a big logic that you want to apply to a current object/data, and you do not wish to make more copies of it (in memory). Hope this helps.

BG Adrian
  • 485
  • 1
  • 4
  • 12