Welcome to WebmasterWorld Guest from 54.196.175.173

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

&= Operator

     

acemaster

5:44 am on May 13, 2009 (gmt 0)

5+ Year Member



I was trying to find the quickest way to check if 4 variables are the same, and accidentally found this perculiar operator. It seems to work, but I'm a bit sceptical since I can't find documentation on it anywhere. Here's a sample set I was working with:

<?
$a = "test";
$b = "test";
$c = "test";
$d = "test";

if ($a == $b &= $c &= $d){
echo "true";
}else{
echo "false";
}
?>

Does anyone use this operator? Can someone provide a link to the documentation on this maybe?

Thanks in advance for your help!

dreamcatcher

6:15 am on May 13, 2009 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Hi acemaster,

Its called 'Passing by Reference'. It means that both your variables point to the same content. This should help you out:

[uk.php.net...]

dc

acemaster

8:54 am on May 13, 2009 (gmt 0)

5+ Year Member



Thanks dc, but it doesn't say anywhere about being able to use it as a comparison operator, at least I couldn't find it.

enigma1

9:37 am on May 13, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



It's not a comparison operator, you are using it as a logical "AND" operator in your code. So if you take this part alone of the if statement $b &= $c is basically $b = $b&$c (logical and assignment)

penders

9:50 am on May 13, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Yes, as enigma1 states. Not to be confused with the similar looking 'assign/pass' by Reference (=&)

&=
... Logical bitwise AND
=&
... 'assign' by Reference

penders

10:11 am on May 13, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Just as a matter of interest, have you tried echo'ing the values of $a, $b, $c and $d after your if() statement (if the values are initially different and not equal)? Something like:

$a = "test1"; 
$b = "test2";
$c = "test3";
$d = "test4";
if ($a == $b &= $c &= $d) {
echo "true";
}else{
echo "false";
}
echo '<br>';
echo "$a - $b - $c - $d";

Results in:

false
test1 - test0 - test0 - test4

Which is not what you require. (Bitwise Operators [php.net])

Just expanding on what enigma1 wrote above... The bitwise assignment operators work from right to left, so what your code is actually doing is equivalent to:

$c = $c & $d; 
$b = $b & $c;
if ($a == $b) { ...

To check if 4 variables are the same you will need to do something like:

if ($a == ($b == ($c == $d))) { 
echo "true";
} else {
echo "false";
}

coopster

11:57 am on May 13, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



That won't work either. Working from within the parenthesis, $c == $d will return boolean
TRUE
as the value of variable $c which is "test" does indeed equal the value of variable $d which is "test". The return value of that expression is boolean
TRUE
. Now you have ...
if ($a == ($b == ((bool) 
TRUE
))) {

... and so the statement will indeed return true.

penders

4:49 pm on May 13, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



if ($a == ($b == ((bool) TRUE))) {

Dang, of course - thanks!

In that case, it's just the usual...

if (($a == $b) && ($b == $c) && ($c == $d)) { 
echo "true\n";
} else {
echo "false\n";
}

Anything 'quicker'?

IMHO, if you need to check for equality between many variables then I'd use an alternative memory structure eg. an array.

coopster

7:11 pm on May 13, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I agree, that was my first thought :)

You could probably get fancy with something like array_diff and array_filter, perhaps array_values thrown in ... just some initial thoughts that raced through my mind.

pinterface

8:24 am on May 14, 2009 (gmt 0)

5+ Year Member



If you want to use an array_* function, you'd probably want array_unique [us.php.net]:
if (1 == sizeof(array_unique([$a, $b, $c, $d]))) {
// ...
}

Something like that.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month