I was going to explain to our intern the difference between "pass by reference" and "pass by value" in PHP, and did this simple script:
$a=5;
$b=&$a;
$a=8;
echo $b;
// prints 8
$a=5;
$b=$a; //no &
$a=8;
echo $b;
// prints 5
However, running this in php-cli using php -qa yields:
php > $a=5;
php > $b=&$a;
php > $a=8;
php > echo $b;
8
php > // prints 8
php > $a=5;
php > $b=$a; //no &
php > $a=8;
php > echo $b;
8
php > // prints 5
Should not the $b=$a; unlink $a and $b?
... so I got curius, and tried:
php > $b=3;
php > echo $a;
3
So, how did I get this wrong? What's going on here? It seems the reference-setting is somehow sticking, even though it should be cleared at the line $b=$a? I also tried:
php > $e=5; $f=$e; $e=6; echo $f;
5
...Which works as expected.
$a and $b seems linked permanently? Am I missing some big point here? How do I "unlink" the $a and $b variable?
unset($b)before the second part?