I'll step through the following snippet - maybe it'll give you a clearer
explaination.
> <?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;
> ?>
1. $foo is assigned the value 'Bob'.
2. an alias to $foo is made. it is $bar. $foo and $bar now both have the
same value, 'Bob'.
3. $bar (and $foo) are assigned a new value; before the assignment takes
place, however, the string to be assigned is evaluated (hence $bar is
still 'Bob' at this point). The value of $bar (and $foo) becomes 'My
name is Bob'.
4. The same value is echo'd twice; thus you get 'My name is BobMy name
is Bob' as your output.
--Toby
Richard Lynch wrote:
>
> It's not a backwards assignment. It's more like this:
>
> $bar = &$foo;
>
> You now have essentially two variable names 'foo' and 'bar' pointing to the
> same chunk of memory inside the computer.
>
> If you change one, you change the other: They are really not two distinct
> variables -- They are the same variables, and are aliases for each other.
>
> --
> Visit the Zend Store at http://www.zend.com/store/
> Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm
> Volunteer a little time: http://chatmusic.com/volunteer.htm
> ----- Original Message -----
> From: Noel Akins <[EMAIL PROTECTED]>
> Newsgroups: php.general
> Sent: Friday, January 26, 2001 10:52 AM
> Subject: [PHP] I'm confused about..reffering vars &$
>
> > 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.
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > To contact the list administrators, e-mail: [EMAIL PROTECTED]
> >
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]