Re: [PHP] Re: reference variables

2009-05-22 Thread Shawn McKenzie
kranthi wrote:
> thank you for the reply.
> 
> i had a small misunderstanding regarding variable reference...now its clear
> 
> but..
> 
> the output of
> 
>  $x = 1;
> $a1 = array(&$x);
> var_dump($a1);
> ?>
> 
> is
> array(1) {
>   [0]=> &int(1)
> }
> 
> while for
> 
>  $x = 1;
> $a1 = array(&$x);
> var_dump($a1[0]);
> ?>
> 
> it is
> 
> int(1)
> 
> can u tell me what & signifies here??
> 

In the first instance you are var_dumping the entire array so it shows
the structure/values of the array elements thus showing a reference to a
var that is int(1).  In the second instance you are var_dumping a
specific var so you get the value of the var int(1).  I'm not sure if
there is a reason that it doesn't show that it is a reference, but the
same is true here:

$x = 1;
$y =& $x;

var_dump($y);

int(1)

-- 
Thanks!
-Shawn
http://www.spidean.com

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



Re: [PHP] Re: reference variables

2009-05-22 Thread Ashley Sheridan
On Fri, 2009-05-22 at 07:44 -0500, Shawn McKenzie wrote:
> kranthi wrote:
> > i have this script
> > 
> >  > $x = 1;
> > $y = 2;
> > $a1 = array(&$x, &$y);
> > $a2 = array($x, $y);
> > $a2[0] = 3;
> > print_r($a1);
> > print_r($a2);
> > ?>
> > 
> > i am expecting
> > 
> > Array
> > (
> > [0] => 3
> > [1] => 2
> > )
> > Array
> > (
> > [0] => 3
> > [1] => 2
> > )
> > 
> > 
> > while i m getting
> > 
> > Array
> > (
> > [0] => 1
> > [1] => 2
> > )
> > Array
> > (
> > [0] => 3
> > [1] => 2
> > )
> > 
> > 
> > any ideas why this is happening?? or am i missing something..?
> > the same is the case when i replace
> > $a2[0] = 3; with
> > $a1[0] = 3;
> > $x = 3;
> > 
> > Kranthi.
> > 
> 
> $a2[0] was assigned the value of $x or 1, so when you change $a2[0],
> that's all that changes.  You have changed the value 1 to 3. $a1[0] is a
> reference to $x, so if you change $a1[0] it will change $x, but not
> $a2[0] because it is not a reference to $x.
> 
> -- 
> Thanks!
> -Shawn
> http://www.spidean.com
> 
To get the results you expect, you should remove the & from the
assignment, as this is assigning by reference, rather than by value like
Shawn said, or use an & for $a2 as well.


Ash
www.ashleysheridan.co.uk


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



Re: [PHP] Re: reference variables

2009-05-22 Thread kranthi
thank you for the reply.

i had a small misunderstanding regarding variable reference...now its clear

but..

the output of



is
array(1) {
  [0]=> &int(1)
}

while for



it is

int(1)

can u tell me what & signifies here??