First, you should define constants for your values:
const TYPE1 = 0b01000000;
const TYPE2 = 0b10000000;
const TYPE3 = 0b00000001;
const TYPE4 = 0b00010000;
const TYPE5 = 0b00000010;
const TYPE6 = 0b00100000;
This will keep you sane. I used binary (0b
notation), but you could use hex (0x
) or decimal just as easily; I think binary makes it easiest to see which bit represents which constant though, and when you're defining new constants you can easily see which bits have already been used. Bear in mind that constants' names are case-sensitive... but just stick with ALL_CAPS constants and you'll save yourself a lot of headache down the road.
Then you can use the logical operator &
to test for each one, as per the comment by @MarkBaker :
if ($mask & TYPE1) echo "Type 1 is set\n";
if ($mask & TYPE2) echo "Type 2 is set\n";
if ($mask & TYPE3) echo "Type 3 is set\n";
if ($mask & TYPE4) echo "Type 4 is set\n";
if ($mask & TYPE5) echo "Type 5 is set\n";
if ($mask & TYPE6) echo "Type 6 is set\n";
For your example of the value 3, you'd get an output of:
Type 3 is set
Type 5 is set
Also, a caveat when working in PHP (and many other languages) -- numbers written with a leading zero, e.g. 032 in your question, are interpreted as octal! Therefore 032
is not actually 32
, but rather 26
!