Re: Variables by Reference
| From: | Robin Vickery | Date: | Mon, 31 Jul 2000 13:55:07 +0000 |
| Subject: | Re: Variables by Reference | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9162@lists.php.net to get a copy of this message | ||
kozer@optushome.com.au (Matt) writes:
> Hello, i'm just wondering, if a variable is created
> by reference, does that mean that the two variables
> are mirrors of each other ? Or does that mean the
> creating variable can only change the other and
> not the other way round.
>
> Eg.
>
>
> $init = "hello world";
>
> $init = &$newref;
echo $newref; // echos "";
echo $init; // echos "";
> $newref = "five";
>
> echo $newref; // echos "five";
> echo $init; // was mirrored, echos five too
>
>
> This is a simple example but i'm sure you all know
> what i'm talking about, so which is it ? Mirrored
> or not ?
You are making two names for the same piece of data, so if you change
the data then it's changed, no matter what you call it.
$foo = "fooble"; // puts "fooble" in $foo
$bar = &$foo; // makes $bar point to the same data as $foo
print "\$foo = '$foo' <br>\n"; // prints "fooble"
print "\$bar = '$bar' <br>\n"; // prints "fooble"
$bar = "barble"; // changes the data that $bar points at to
"barble"
print "\$foo = '$foo' <br>\n"; // prints "barble"
print "\$bar = '$bar' <br>\n"; // prints "barble"
$foo = "bazzle"; // changes the data that $foo points at to
"bazzle"
print "\$foo = '$foo' <br>\n"; // prints "bazzle"
print "\$bar = '$bar' <br>\n"; // prints "bazzle"
--
Robin Vickery...............................................
Planet-Three, 3A West Point, Warple Way, London, W3 0RG, UK
Email: robin@planet-three.net Phone: +44 (0)794 670 6395