In PHP, reference is NOT pointer. It is something like alias of another variable. I will explain what happen with your code:
$one = 1;
$two = 2;
$ref = &$one;
After three commands above, we have:
variables | $one | $ref | $two |
content | 1 | 2 |
As you see, $one and $ref refer to the same content, it is what the term reference meaning. Continue:
global $ref, $two;
According to this document, above command the same as:
$ref =& $GLOBALS['ref'];
$two =& $GLOBALS['two'];
So, we have:
variables | $one (global) | $ref(global) | $ref (local) | $two (global) | $two (local) |
content | 1 | 2 |
Yes, we have 5 variables! Continue:
$ref = &$two;
It is actually:
$ref (local) = &$two (local);
So we have:
variables | $one (global) | $ref(global) | $ref (local) | $two (global) | $two (local) |
content | 1 | 2 |
And, the last command:
echo $ref;
Actually is:
echo $ref (global);
And, 1 is the correct value!
Additional:
change();
echo $two;
function change(){
global $ref, $two;
$ref = &$two;
$ref = 9;
}
The result of this code is 9;
----- EDIT -----
I did not read the question carefully. My answer is for the part The result of the code is "1". I don't really understand why. The answer of Jonathan Gagne is what you are looking for.
$ref = $two;