$a = array (1, 2, 3, 4);
foreach ($a as &$b) $b++;
foreach ($a as $b) $b++;
print_r ($a);
It is increment twice because after the first loop, $b is still a pointer to the fourth element of $a. Continuing in the code, the second loop will assign the fourth value of $a each value of $a, then increment it. Try debugging it like so:
$a = array (1, 2, 3, 4);
foreach($a as &$b){
$b++;
}
print_r($a);
foreach($a as $b){
print_r($a);
$b++;
print_r($a);
}
print_r($a);
... and you will see what it is doing.
Solution: unset your variables before you re-use them.
$a = array (1, 2, 3, 4);
foreach ($a as &$b) $b++;
unset ($b)
foreach ($a as $b) $b++;
print_r ($a);
And for the $$ thing, it seems to do exactly what it is intended for...
[Simple]
$a = "Hello World";
$b = "a";
echo $$b;
//outputs 'Hello World'
[Complex]
$var0 = "Hell";
$var1 = "o wo";
$var2 = "rld!";
$i = "var0";
while(intval($i[3]) <= 2){
echo $$i;
$i[3] = intval($i[3])+1;
}
//outputs 'Hello World!'