You can use bitwise operators .
For instance:
$bits = bindec('0110');
var_dump($bits & bindec('0100'));
You will get "4" (i.e. a non-zero value, which you can consider as "ok"), since the third bit is set to $bits.
AND:
$bits = bindec('0010');
var_dump($bits & bindec('0100'));
You will get 0 - since the third bit is $bitsnot set.
Of course, you do not need to call every time bindec- I just used it here to simplify the reading; my first example can be rewritten somehow like this:
$bits = bindec('0110');
var_dump($bits & 1<<2);
, , ; :
define('PERMISSION_TO_DO_X', 1);
define('PERMISSION_TO_DO_Y', 1<<1);
define('PERMISSION_TO_DO_Z', 1<<2);
:
$bits = bindec('0110');
if ($bits & PERMISSION_TO_DO_X) {
echo "OK to do X<br />";
}
if ($bits & PERMISSION_TO_DO_Y) {
echo "OK to do Y<br />";
}
if ($bits & PERMISSION_TO_DO_Z) {
echo "OK to do Z<br />";
}
:
OK to do Y
OK to do Z
: