homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

&= Operator

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

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";
echo "false";

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

Thanks in advance for your help!



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

Hi acemaster,

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




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

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.


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

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)


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

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

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


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

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";
echo "false";
echo '<br>';
echo "$a - $b - $c - $d";

Results in:
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";


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

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.


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

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.


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

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.


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

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved