Re: problem: creating global alias inside function
| From: | Tom Rogers | Date: | Tue, 29 Jul 2003 02:45:52 +0000 |
| Subject: | Re: problem: creating global alias inside function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-157267@lists.php.net to get a copy of this message | ||
Hi,
Tuesday, July 29, 2003, 12:32:33 AM, you wrote:
MP> The following code illustrates a problem I've got with references (running
MP> on PHP 4.3.2). Can anyone explain it for me? Thanks in advance for any
MP> assistance!
MP> Martin
MP> <?php
MP> $globalvariable = 0;
MP> $one = 1;
MP> //want to set up $globalvariable as a reference to $one
MP> setglobal($one);
MP> echo $globalvariable; //prints 0, not 1
MP> function setglobal(&$one)
MP> {
MP> global $globalvariable;
MP> $globalvariable =& $one;
MP> }
?>>
You don't need the =& in the function as you are passing a reference
already. So you are setting $globalvariable to point to a reference
that is pointing to $one. You just need:
function setglobal(&$one)
{
global $globalvariable;
$globalvariable = $one;
}
--
regards,
Tom