References

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

References

Post by evilmonkey »

Hello,

Can someone please explain to me (preferably in simple English) what references are? I looked at the PHP manual, but I don't understand a thing. I am especially interested in the "&" symbol and what it allows me to do. From what I can see,

Code: Select all

$x=5;
$y = $x;
and

Code: Select all

$x=5;
$y =& $x;
produces identical results...

Thanks!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

consider this:

Code: Select all

$x = 5;
$y = $x;
var_dump($x, $y);
$y = 15;
var_dump($x, $y);
and this:

Code: Select all

$x = 5;
$y =& $x;
var_dump($x, $y);
$y = 15;
var_dump($x, $y);
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Read the complete scoop on the & sign here:

http://us3.php.net/manual/en/language.r ... es.whatare
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

In simple english:
after executing

Code: Select all

$y =& $x;
$y is an alias for $x. Any further changes to $y would be reflected to $x as well
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Weirdan wrote:In simple english:
after executing

Code: Select all

$y =& $x;
$y is an alias for $x. Any further changes to $y would be reflected to $x as well
Yes, that is exactly the explanation I needed. I was considering putting "in simple English or Russian", but decided not to, didn't realize there were people who spoke Russian here. Spasibo! :D

Oh, one more thing. Are changes to $x reflected on $y?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

evilmonkey wrote: Yes, that is exactly the explanation I needed. I was considering putting "in simple English or Russian", but decided not to, didn't realize there were people who spoke Russian here. Spasibo! :D
Bez problem :P
[Moderator's hat on]
However we speak English here :twisted:
[Moderator's hat off]
evilmonkey wrote: Oh, one more thing. Are changes to $x reflected on $y?
Yes, they are. In fact when $y is a reference to $x you could use both interchangeably.
Last edited by Weirdan on Thu Nov 24, 2005 10:59 am, edited 2 times in total.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

And an amerpsand in a function parameter like:

Code: Select all

$somevar = 5;
    function foobar($somevar){
        //this is called passing by reference
        //What ever you do to $somevar in the function is reflected outside the function
        $somevar = 12;
   }
    foobar(&$somevar);
   echo $somevar;  //outputs 12
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

neophyte wrote:

Code: Select all

foobar(&$somevar);
This syntax would cause warnings:
Warning wrote: PHP Warning: Call-time pass-by-reference has been deprecated - argument passed
by value
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Wouldn't you know it...

So correct would be:

Code: Select all

$somevar = 5;
function foobar(&$somevar){
   //function stuff
   
}

foobar($somevar);
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Alright, that more or less clears things up. Thanks guys!
Post Reply