View Single Post
Old 05-06-2009, 08:29 PM   #1 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default pass by reference

I know I know...I keep digging up old articles/tutorials and threads and asking about them, but just when I thought I understood pass by ref, I found Wildhoney's article and it confused me somewhat...

His link:

http://www.talkphp.com/vbarticles.ph...s-as-reference



Anyways, I understand what is happening below:

PHP Code:
$szVar1 'TalkPHP.com';
$szVar2 'WiredFlame.com';

$szVar1 =& $szVar2

$szVar1 is NOT being assigned the value of $szVar2, it is simply being redirected or pointed to its value, which is 'WiredFlame.com'.

Print this out and you get:

szVar1 is: WiredFlame.com
szVar2 is: WiredFlame.com


Now.......

PHP Code:
$szVar1 'TalkPHP.com';
$szVar2 'WiredFlame.com';

$szVar1 =& $szVar2;
$szVar1 'TalkPHP.com'
Print this out and you get:

szVar1 is: TalkPHP.com
szVar2 is: TalkPHP.com



Could someone please explain to me, how is it that simply passing by reference through $szVar2 changes it? I thought it was a one-way street....

$szVar1 --> I want YOUR value --> $szVar2

$szVar2 (OK) --> Here, I point you to my value --> $szVar1 (thanks)

I don't understand how you can "control" $szVar2 just because you are passing by reference through it...

My logical thinking (which is wrong) would be that $szVar1 would be: TalkPHP.com

But it would leave $szVar2 untouched, and still assigned the value: WiredFlame.com


Also, what is the better practice of using the "&" sign?

$szVar1 =& $szVar2;

or

$szVar1 = &$szVar2;
allworknoplay is offline  
Reply With Quote