Initially, I found bitmasking to be a confusing concept and found no use for it. So I've whipped up this code snippet in case anyone else is confused:
<?php
$hasFourWheels = 1;
$hasTwoWheels = 2;
$hasDoors = 4;
$hasRedColour = 8;
$bike = $hasTwoWheels;
$golfBuggy = $hasFourWheels;
$ford = $hasFourWheels | $hasDoors;
$ferrari = $hasFourWheels | $hasDoors | $hasRedColour;
$isBike = $hasFourWheels & $bike; $isGolfBuggy = $hasFourWheels & $golfBuggy; $isFord = $hasFourWheels & $ford; ?>
And you can apply this to a lot of things, for example, security:
<?php
$writePost = 1;
$readPost = 2;
$deletePost = 4;
$addUser = 8;
$deleteUser = 16;
$administrator = $writePost | $readPosts | $deletePosts | $addUser | $deleteUser;
$moderator = $readPost | $deletePost | $deleteUser;
$writer = $writePost | $readPost;
$guest = $readPost;
function checkPermission($user, $permission) {
if($user & $permission) {
return true;
} else {
return false;
}
}
if(checkPermission($administrator, $deleteUser)) {
deleteUser("Some User"); }
?>
Once you get your head around it, it's VERY useful! Just remember to raise each value by the power of two to avoid problems