Never seen this before

Hi guys,

I’m looking through some legacy code created by another ‘developer’ and I use the term developer very loosely.

I may be missing the obvious here, but I’ve come accross the following code and i’ve never seen the & operator used like it is.

IF( !($obj->status & CONSTANT_NAME) ) { ... }

What does “$foo & $bar” do?
Is it sort of an equals operator?

Also, i’ve came across a MySQL query containing something similar

(status & ".CONSTANT_1." = 0 AND status & ".CONSTANT_2." = 0)

What does this one do?..

I can see myself being very embarrassed very soon…

Cheers.

Never used it myself, but here the manual talks about it: http://www.php.net/manual/en/language.operators.bitwise.php

Thanks for the link.
So… It’s sort of a comparison?

not sure what exactly you mean by " I use the term developer very loosely." buy if you don’t know what the & means, then you are hardly a “real developer” yourself.
These types of operators are usually some of the first things they teach in computer science classes and in every programming training courses.

Do you know what the && operator means? It actually the optimized version of the &, which means it basically works just like the & but will not even try a second evaluation if the first one fails.

But in that specific case you mentioned it means something like this:
If the bits of the ‘status’ also exist in the CONSTANT_NAME then condition is true.

The ‘status’ and CONSTANT_NAME don’t have to be the same, but all the bits in the CONSTANT_NAME must also be present in the ‘status’
This is a very common way to set option flags and it’s extremely fast to compare. If only more developers were using the bitwise flags for options, the world would be a better place

For example, any odd number & 1 is true, while any even number & 1 is false

The reason I said I use the term loosely is because the guy before my predecessor was appalling based on his code and lack of common sense and planning.

I use the && operator all the time. I’ve just never came across the & operator in the past whether it be a tutorial, book or anyone else’s code hence my reason for asking what it’s purpose is.

Actually the && is an “optimized” version of the & operator.
The optimization is this: if the first parameter evaluates to false, the evaluation immediately returns false and second is never evaluated.
This makes it possible to do this like this:
if(is_object($o) && $o->status === GOOD){
// do stuff
}

Here if the $o is not an object the script will never get to $o->status, if it did, it would have resulted in parse error.

You can also use this optimization trick to your advantage. For example, when you need to evaluate multiple conditions, always put the ones that most likely to fail first.

The same is true with the || operator, which is an optimized version of the bitwise | operator, only it returns true if the first condition is true.

This is a very common optimization and it’s used in many languages, even the XSLT standards require XSL parsers to use this type of optimization for && and || evaluations.
JavaScript goes even further, returning the result of the first condition (not just true or false).