NYCPHP Meetup

NYPHP.org

[nycphp-talk] newbie confusion

Chris Bielanski Cbielanski at inta.org
Thu Nov 11 09:28:59 EST 2004


Code examples aside, my favorite analogy regarding the difference between
value and reference passing is:

"A variable is like a piece of smail-mail, it comes with two parts: The
letter (or value) and the envelope (or address). When you open the envelope
and give someone the letter inside, you are passing by value (or assigning).
It's the same as the "=" operation. When you give someone the envelope, you
are passing by reference. That's the operation done by "=&" and its up to
them to dereference the mail - to open the envelop to see what's inside."

Maybe someone will find that useful...

Thanks,
Chris Bielanski
Web Programmer, 
International Trademark Association,
1133 Avenue of the Americas, 33rd Floor
New York, NY 10036
+1 (212) 642-1745, f: +1 (212) 768-7796
mailto:cbielanski at inta.org, www.inta.org  
INTA -- 125 Years of Excellence



> -----Original Message-----
> From: Joseph Crawford [mailto:codebowl at gmail.com]
> Sent: Thursday, November 11, 2004 9:15 AM
> To: NYPHP Talk
> Subject: Re: [nycphp-talk] newbie confusion
> 
> 
> > I'm curious to know what the "&" is for in the statement below?
> > 
> > -Aaron
> 
> 
> Let me explain a bit and try to make it so you understand.  In PHP &
> is the reference operator, this allows you to pass a variable by
> reference rather than by value.  Here is an example
> 
> $var = 4;
> func( $var );
> // echo's 4
> echo $var;
> 
> function func( $var ) {
>     $var++;
>     // echo's 5
>     echo $var;
> }
> 
> if you run this code, you will see how the values of $var are
> different inside the function and outside the function.  The one
> outside the function is 4 and the one inside is 4.  When you pass by
> value php defaults to making a copy of the variable.
> 
> When you use the & (reference operator) it will pass $var by reference
> rather than by value, this passes the memory address rather than
> making a copy of the variable.  Let me show anoher example.
> 
> $var = 4;
> func( &$var );
> // echo's 5
> echo $var;
> 
> function ( $var ) {
>     $var++;
> }
> 
> If i am correct about this (which i believe i am) when you echo $var
> after the function call, $var now equals 5 because you passed the
> variable by reference rather than letting PHP make a copy.  You need
> to be careful passing variables/objects by reference, if you do not
> intend to have the function change the value of the variable outside
> of the functions scope, do not pass by reference.
> 
> I hope you understood everything i have said, if you do not, please
> feel free to ask any questions reguarding my reply.
> 
> 
> -- 
> Joseph Crawford Jr.
> Codebowl Solutions
> codebowl at gmail.com
> 
> For a GMail account
> contact me OFF-LIST
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
> 



More information about the talk mailing list