Re: If this is the expected behavior references suck
| From: | Joao Prado Maia | Date: | Wed, 04 Oct 2000 21:59:16 +0000 |
| Subject: | Re: If this is the expected behavior references suck | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18664@lists.php.net to get a copy of this message | ||
On Wed, 4 Oct 2000, Mike Heath wrote:
> Is this the expected behavior? From reading the documentation shouldn't $a
> and $b both point to the same variable and by changing $a shouldn't it also
> change $b? ...or is there something wrong with my logic here? I'm using
> PHP 4.0.2 compiled statically with Apache 1.3.12 on RedHat 7.0.
>
> <?php
>
> function foo(&$var) {
> global $a;
> $a = &$var;
> }
>
> $b = "Start";
> foo($b);
> $a = "End";
> echo $b; // Shouldn't this echo "End" not "Start"?
>
> ?>
>
Well, there are so many things wrong in there it's even hard to start. If
you want to change the value of the $b, try this:
<?php
function foo(&$var) {
global $a;
$var = $a;
}
$b = "Start";
$a = "End";
foo(&$b);
echo $b;
?>
You don't use the ampersand inside the function, only in function
calls. Anyway, I hope this can help you.
Regards,
Joao
--
Joao Prado Maia
Web Developer
Kefta, Inc.
T: (415) 391-6881 ext 8008
F: (415) 391-7079