atitthaker Posted September 21, 2006 Share Posted September 21, 2006 What is the difference between =& and = in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/21498-difference-between-and/ Share on other sites More sharing options...
trillion Posted September 21, 2006 Share Posted September 21, 2006 http://academ.hvcc.edu/~kantopet/old/php/index.php?page=php+variables&parent=php+basics Quote Link to comment https://forums.phpfreaks.com/topic/21498-difference-between-and/#findComment-95861 Share on other sites More sharing options...
warewolfe Posted September 21, 2006 Share Posted September 21, 2006 Hej = is an assignment operator, a variable is assigned a value, =& is a reference operator, two variables reference one value. $varA = 1; $varA now has the value of 1$varA =& $varB $varA now has the value of $varB, whatever that is. Quote Link to comment https://forums.phpfreaks.com/topic/21498-difference-between-and/#findComment-95862 Share on other sites More sharing options...
.josh Posted September 21, 2006 Share Posted September 21, 2006 okay to expand on that, let's look at this example:[code]$a = 'foo';$b =& $a;echo $a; // output: fooecho $b; // output: foo[/code]okay at face value, it assigns the value of $a to $b. But what is really happening here is that you are assigning the reference pointer of $a to $b. At this given point in time, they are both holding the value 'foo'. Now let's look expand this example:[code]$a = 'foo';$b =& $a;echo $a; // output: fooecho $b; // output: foo$a = 'bar';echo $a; // output: barecho $b; // output: bar$b = 'foobar';echo $a; // output: foobarecho $b; // output: foobar[/code]same thing as before, at first. but now when we assign 'bar' to $a, notice how echoing $a and $b both echo 'bar', instead of $b echoing 'foo'. it is because the initial =& assigns the same memory location reference to both variables. And again, if i were to go and assign 'foobar' to $b, you will see that they both now echo 'foobar'both variables point to the same memory location. changing one effectively changes the other, because the other one points to the same place, which you changed with the other. Quote Link to comment https://forums.phpfreaks.com/topic/21498-difference-between-and/#findComment-95863 Share on other sites More sharing options...
atitthaker Posted September 21, 2006 Author Share Posted September 21, 2006 Thank you all for the help. :) Quote Link to comment https://forums.phpfreaks.com/topic/21498-difference-between-and/#findComment-95954 Share on other sites More sharing options...
Barand Posted September 21, 2006 Share Posted September 21, 2006 BTW, Another common use for the & is for passing arguments to functionsIn this first example, $foo is passed "by value" so the variable sent to the function is a copy of the original[code]<?phpfunction f1 ($foo) { $foo *= 2; return $foo;}$foo = 3;$bar = f1($foo);echo $foo; // 3echo $bar; // 6?>[/code]In this example it is passed "by reference" so a pointer to the original memory location is sent[code]<?phpfunction f2 (&$foo) { $foo *= 2; return $foo;}$foo = 3;$bar = f2($foo);echo $foo; // 6echo $bar; // 6?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21498-difference-between-and/#findComment-95993 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.