& is a bitwise operator. It combines two bitwise values.
What is a bitwise operator?
Each integer is internally represented as several bits.
1 is 0001 2 is 0010 4 is 0100 8 is 1000
And so on. each bit value is two times larger than the previous one.
You can get other numbers by combining bits
3 is 0011 (2+1) 5 is 0101 (4+1)
Bitwise operation works with each bit in both variables. & sets each bit as a result of 1 if it is 1 in both the values ββit works on.
9 & 5 == 1
because
9 == 1001 5 == 0101 ---------- 1 == 0001
| there will be a COMBINE of all 1s:
3 | 5 == 7
3 == 0011 5 == 0101 --------- 7 == 0111
How can you use it?
Example:
define('LOG_WARNING',1); define('LOG_IO',2); define('LOG_ALIENATTACKS,4); $myLogLevel = LOG_WARNING | LOG_ALIENATACKS;
Now $myLogLevel is a combination of LOG_WARNING and LOG_ALIENATTACK . You can check it with the & operator:
if($myLogLevel&LOG_WARNING).... //true if($myLogLevel&LOG_IO).... //false if($myLogLevel&LOG_ALIENATTACKS)..../ /true run or your live!!!
If you want to know more about topic search for bit flags and binary operations.
source share