I'm confused about..reffering vars &$
| From: | Noel Akins | Date: | Thu, 25 Jan 2001 21:30:18 +0000 |
| Subject: | I'm confused about..reffering vars &$ | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-36798@lists.php.net to get a copy of this message | ||
Ok, I'm reading the manual for the new php4 stuff. Now, I'm not new to php. I started with php/fi with a little script to add links to a database. I did a little work in php3. I just don't make enough php stuff to get a frim grip on everything. That is changing as I am building a second pc to serve as a dev box for php4. Here is my first question.
This was lifted from the Zend site from the php manual there.
To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable). For instance, the following code snippet outputs 'My name is Bob' twice:
<?php
$foo = 'Bob'; // Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
$bar = "My name is $bar"; // Alter $bar...
echo $foo; // $foo is altered too.
echo $bar;
?>
What I don't see here is how this prints (echo) "My name is Bob". The way I see this is that $bar = &$foo; is a backwards assignment, so that when the script cycles through, it changes $foo from Bob to "My name is $bar", and, to my error prone thinking, it should print out "My name is My name is My name is...".
It would make sense to me if it were:
<?php
$foo = 'Ed'; // Assign the value 'Ed' to $foo
$bar = &$foo; // Reference $foo via $bar
$bar = 'Bob'; // changing $foo to 'Bob'
$this = "My name is $bar";
$that = "My name is $foo";
echo $this; / / prints out My name is Bob
echo $that; / / prints out My name is Bob
Maybe I'm not seeing how php rereads or cycles through the script once it sees a "assign by reference" Could someone explain please?
Thank you.