I was going to explain to our intern the difference between "pass by reference" and "pass by value" in PHP, and made 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, doing this in php-cli using php -qa gives:
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 $b=$a; disable $ a and $ b?
... so I got curius and tried:
php > $b=3; php > echo $a; 3
So how do I get it wrong? What's going on here? It seems that the reference setting is somehow sticking, although it should be cleared on the line $b=$a ? I also tried:
php > $e=5; $f=$e; $e=6; echo $f; 5
... works as expected.
$a and $b seem to be connected all the time? Did I miss any big point here? How to "unlink" with the variable $a and $b ?
source share