Re: References to a variable and scope

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2007-12-04 at 16:58 -0400, Cesar D. Rodas wrote:
> Hello,
> 
> I know that PHP doesn't support pointers to a variable, instead of that
> there is references to a variable which are similar to pointers, right?
> 
> BTW, what I want to do is to save a references to a variable and read the
> content when I need, similar to PDO "bindParam". I will try to explain
> better in the following pseudo php code.
> 
> function foo($a) {
>        $GLOBALS['references']['a'] = /*references to $a */
> }
> 
> function bar() {
>       echo $GLOBALS['references']['a'];
> }
> 
> $var="hello"
> foo($var);
> $var = "hi";
> bar(); /* it should print "hi" instead of "hello" */

Your code is broken:

<?php

function foo( &$a )
{
    $GLOBALS['references']['a'] = &$a; /*references to $a */
}

function bar()
{
      echo $GLOBALS['references']['a'];
}

$var="hello"
foo($var);
$var = "hi";
bar(); /* it should print "hi" instead of "hello" */

?>

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux