Not sure if AKAPJ mentions this or not or if it's even relevent, but...
That operator has dual meaning in PHP and depends on it's context...It's also the bitwise AND operator.
Code: Select all
$boo = 'This is a string';
$foo = &$boo; // $foo now points too $boo
Whenever you change $foo or $boo, the change is applied to both variables, because $foo is a reference of $boo.
It's not a copy, but a reference. Normally if you used the above code *without* the & operator $foo would become a "copy" of the $boo variable, because you basically assign the contents of $boo to $foo. However using the & operator the assignment now takes a slightly different role, in that it doesn't copy the "content" but rather the location or memory address of $boo into the reference variable $foo.
Basically $foo has become a special kind of variable called a reference. You will often hear people use words like:
Copy by reference
Copy by value
The latter is what happens *without* the & operator and the former is what happens when you make the assigned variable a reference variable.
Consider the following:
(1200)$foo = 'Hello world';
(1212)$boo = $foo;
(1200)$boo = &$foo;
The brackets with the 4 digits are random memory addresses PHP assigns each variable.
The first line the variable $foo is assigned the "content"
Hello World and it starts at the memory address
1200 and takes up 12 bytes.
The first variable $boo is a copy of $foo and therefore also takes up 12 bytes. togather these two variables take up 24 bytes (could be more or less but thats not what matters)
The second variable $boo is a special variable called a reference. It doesn't copy the 'contents' of $foo but instead copies the 'address' of $foo so this instance of $boo doesn't take up any memory (except for itself - but again irrelevant).
It simply 'points' to the memory location of the variable it references. Thats why it's 4 digit address inside the brackets is the same as the original $foo.
Secondly, the & operator has the meaning of bitwise AND operator...it can be used to check whether bits in a variable are set. For instance:
Code: Select all
$var = 10; // Binary = 0000 1010
$var = ($var & 1); // Check if first bit of var is set and assign to itself
After the above operation
$var would contain 0 because the first bit is not set.
Code: Select all
$var = &$var; // Assign a reference to itself (not sure of the results of that :? )
$var = $var & 1; // Test is first bit is set