Difference between "=" and "&="

Discussion forum for MOD Writers regarding MOD Development.
Locked
User avatar
1234homie
Registered User
Posts: 439
Joined: Fri Sep 26, 2008 3:17 pm

Difference between "=" and "&="

Post by 1234homie »

Like in topic title.. What's the difference between using "=" and "&="?

Example:
foreach ($ar as $k => $v)
{
$row &= $rowset[$k];
[...]
}
foreach ($ar as $k => $v)
{
$row = $rowset[$k];
[...]
}
Any help will be appreciated ;d
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52173
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Difference between "=" and "&="

Post by Brf »

&= is "assign by reference".

In that case, $row is actually pointing to the same location in memory as $rowset[$k] instead of making a copy of it.
User avatar
imkingdavid
Former Team Member
Posts: 2673
Joined: Sun Jul 26, 2009 7:59 pm
Location: EST
Name: David King

Re: Difference between "=" and "&="

Post by imkingdavid »

Brf is correct. Here's a little more indepth explanation.

The way I understand it, every variable points to a location within the memory. Let's say you have the following script:

Code: Select all

<?php

$v1 = 'foo';
$v2 = $v1;
$v1 = 'bar';

echo $v2;
// Output: foo
?>
What that did was assign 'foo' to $v1 and then copied the value of $v1 into $v2. However, $v2 points to a separate location in memory. When you change $v1, $v2 remains the same.

Now look at this:

Code: Select all

<?php

$v1 = 'foo';
$v2 &= $v1;
$v1 = 'bar';

echo $v2; // Output: bar
?>
This time, instead of making a new memory location for $v2, we assigned $v2 to point to the SAME location as $v1. In other words, $v1 and $v2 are the same thing, just with different names. Anything done to one is done to the other.
Don't forget to smile today. :)
Please do NOT contact for support via PM or email.
User avatar
1234homie
Registered User
Posts: 439
Joined: Fri Sep 26, 2008 3:17 pm

Re: Difference between "=" and "&="

Post by 1234homie »

ok, now situation is being more clearly. thanks for help.

btw. something like $row =& $rowset[$k] will work in the same way?
Oleg
Former Team Member
Posts: 1221
Joined: Sat Jan 30, 2010 4:42 pm
Location: NYC
Contact:

Re: Difference between "=" and "&="

Post by Oleg »

References in php are tricky to get right and are best avoided unless you are very sure you need them.
Participate in phpBB development: Get involved | Issue tracker | Report a bug | Development board | [url=irc://chat.freenode.net/phpbb-dev]Development IRC chat[/url]
My stuff: mindlinkgame.com
User avatar
AmigoJack
Registered User
Posts: 5799
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Difference between "=" and "&="

Post by AmigoJack »

Sorry, you all mixed it up. :lol:
  1. $a &= $b is the shortcut to $a= $a& $b and as such a bitwise operator.
  2. $a =& $b is confusing and better written as $a= &$b, as these are two operators - the whole operation as such is called reference assignment.
If you look up /viewtopic.php you'll only find $row [color=#00F]=&[/color] $rowset[$post_list[$i]];, not $row [color=#00F]&=[/color] $rowset[$post_list[$i]];.
  • The worst thing about censorship is ███████████
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52173
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Difference between "=" and "&="

Post by Brf »

Aha. You are correct. Putting an operator before the "=" is the shortcut syntax.

$a &= $b;
is the same as
$a = ($a & $b);

I was confusing it with the =& when I said it was the ref operator.
User avatar
AmigoJack
Registered User
Posts: 5799
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Difference between "=" and "&="

Post by AmigoJack »

Brf wrote:the ref operator
...does not exist. While in these cases:

Code: Select all

$a+= 1;  // Increment by one
$b&= 0xFF;  // Only affect rightmost 8bit
$c/= 3;  // Divide by three    
...we have one operator per line, the expression =& is a combination of the assignment operator and a reference treatment. References have nothing to do with operators - it's as faulty as talking about "if-loop"s. ;)
  • The worst thing about censorship is ███████████
  • "The problem is probably not my English but you do not want to understand correctly. ... We will not come anybody anyway, nevertheless, it's best to shit this." Affin, 2018-11-20
  • "But this shit is not here for you. You can follow with your. Maybe the question, instead, was for you, who know, so you shoved us how you are." axe70, 2020-10-10
User avatar
imkingdavid
Former Team Member
Posts: 2673
Joined: Sun Jul 26, 2009 7:59 pm
Location: EST
Name: David King

Re: Difference between "=" and "&="

Post by imkingdavid »

AmigoJack wrote:Sorry, you all mixed it up. :lol:
:oops: Not the first time, either. Thanks for pointing it out.
Don't forget to smile today. :)
Please do NOT contact for support via PM or email.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 52173
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: Difference between "=" and "&="

Post by Brf »

pot-a-to pot-ah-to
Locked

Return to “[3.0.x] MOD Writers Discussion”