Jump to content


Photo

difference between =& and =


  • Please log in to reply
5 replies to this topic

#1 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 21 September 2006 - 04:39 AM

What is the difference between =& and = in PHP?
Beginner with PHP and need help

#2 trillion

trillion
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 21 September 2006 - 04:46 AM

http://academ.hvcc.e...rent=php basics

#3 warewolfe

warewolfe
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts
  • LocationOtago, NZ

Posted 21 September 2006 - 04:52 AM

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.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 September 2006 - 05:05 AM

okay to expand on that, let's look at this example:

$a = 'foo';
$b =& $a;

echo $a; // output: foo
echo $b; // output: foo
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:

$a = 'foo';
$b =& $a;

echo $a; // output: foo
echo $b; // output: foo

$a = 'bar';

echo $a; // output: bar
echo $b; // output: bar

$b = 'foobar';

echo $a; // output: foobar
echo $b; // output: foobar

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. 
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 21 September 2006 - 09:35 AM

Thank you all for the help. :)
Beginner with PHP and need help

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 September 2006 - 11:39 AM

BTW, Another common use for the & is for passing arguments to functions

In this first example, $foo is passed "by value" so the variable sent to the function is a copy of the original
<?php
function f1 ($foo) {
    $foo *= 2;
    return $foo;
}

$foo = 3;
$bar = f1($foo);

echo $foo;        // 3
echo $bar;        // 6
?>

In this example it is passed "by reference"  so a pointer to the original memory location is sent
<?php
function f2 (&$foo) {
    $foo *= 2;
    return $foo;
}

$foo = 3;
$bar = f2($foo);

echo $foo;        // 6
echo $bar;        // 6
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users