<p>I've tried to do this several times with no luck.  After reading <a href="https://stackoverflow.com/questions/1451/what-is-the-best-way-to-handle-multiple-permission-types#1477">this post</a>, it made me interested in doing this again.  So can anyone tell me why the following doesn't work?</p>

<pre><code>&lt;?php

$guest = 1;
$editor = 2;
$admin = 4;

$user = $editor;

if( $user == ($editor | $admin) ) {
    echo "Test";    
}

?&gt;
</code></pre>

[{'answer_id': 17173, 'author': 'Espen Herseth Halvorsen', 'author_id': 1542, 'author_profile': 'https://Stackoverflow.com/users/1542', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s been a long time since I used PHP, but I will assume that this will work:</p>\n\n<pre><code>&lt;?php\n\n    $guest = 1;\n    $editor = 2;\n    $admin = 4;\n\n    $user = $editor;\n\n    if( ($user == $editor) || ($user == $admin) ) {\n        echo "Test";    \n    }\n\n?&gt;\n</code></pre>\n'}, {'answer_id': 17178, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 5, 'selected': True, 'text': '<p>Use the bitwise OR operator (|) to set bits, use the AND operator (&amp;) to check bits. Your code should look like this:</p>\n\n<pre><code>&lt;?php\n\n    $guest = 1;\n    $editor = 2;\n    $admin = 4;\n\n    $user = $editor;\n\n    if( $user &amp; ($editor | $admin) ) {\n        echo "Test";    \n    }\n\n?&gt;\n</code></pre>\n\n<p>If you don\'t understand binary and exactly what the bitwise operators do, you should go learn it. You\'ll understand how to do this much better.</p>\n'}, {'answer_id': 17180, 'author': 'mk.', 'author_id': 1797, 'author_profile': 'https://Stackoverflow.com/users/1797', 'pm_score': 1, 'selected': False, 'text': '<p>(2 | 4) is evaluating to 6, but 2 == 6 is false. </p>\n'}, {'answer_id': 17193, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 1, 'selected': False, 'text': '<p>@mk: (2 | 4) evaluates to 6.</p>\n'}, {'answer_id': 17196, 'author': 'conmulligan', 'author_id': 1467, 'author_profile': 'https://Stackoverflow.com/users/1467', 'pm_score': 1, 'selected': False, 'text': '<pre><code>$guest = 1;\n$editor = 2;\n$admin = 4;\n\n$user = $editor;\n\nif (user == $editor || $user == $admin) {\n    echo "Test";\n}\n</code></pre>\n'}, {'answer_id': 17214, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>Awesome, this seems like the best way to do permissions in a CMS. Yes? No?</p>\n</blockquote>\n\n<p>Maybe, I\'ve never really done it that way. What I have done is used bitwise operators to store a whole bunch of "yes or no" settings in a single number in a single column in the database.</p>\n\n<p>I guess for permissions, this way would work good if you want to store permissions in the database. If someone wants to post some content, and only wants admins and editors to see it, you just have to store the result of</p>\n\n<pre><code> ($editor | $admin)\n</code></pre>\n\n<p>into the database, then to check it, do something like</p>\n\n<pre><code> if ($user &amp; $database_row[\'permissions\']) {\n       // display content\n } else {\n       // display permissions error\n }\n</code></pre>\n'}, {'answer_id': 17220, 'author': 'Espen Herseth Halvorsen', 'author_id': 1542, 'author_profile': 'https://Stackoverflow.com/users/1542', 'pm_score': 0, 'selected': False, 'text': "<p>In my opinion this doesn't scale well. I haven't actually tried using it on a large scale project, but a CMS sounds way to complicated to use this on.</p>\n"}, {'answer_id': 17351, 'author': 'dragonmantank', 'author_id': 204, 'author_profile': 'https://Stackoverflow.com/users/204', 'pm_score': 3, 'selected': False, 'text': '<p>In the interest of not reinventing the wheel, why not take a look at ACL/Authentication systems like <a href="http://framework.zend.com/manual/en/zend.acl.html" rel="noreferrer">Zend ACL</a> and <a href="http://framework.zend.com/manual/en/zend.auth.html" rel="noreferrer">Zend Auth</a>? Both can be used independently from the Zend Framework as a whole. Access Control is a tricky situation so it benefits one to at least look at how other systems do it.</p>\n'}, {'answer_id': 26382, 'author': 'Till', 'author_id': 2859, 'author_profile': 'https://Stackoverflow.com/users/2859', 'pm_score': 0, 'selected': False, 'text': '<p>It always depends on what you need. If you know the Zend Framework already, then I\'d second the Zend_Acl/_Auth suggestion which was made earlier. But keep in mind that every framework propably comes with a similar component.</p>\n\n<p>The other thing that comes to mind is <a href="http://pear.php.net/package/LiveUser" rel="nofollow noreferrer">LiveUser</a>. I like working with it a lot as well.</p>\n\n<p>I think you can do pretty much anything and while your approach looks very simple, it\'s also limited since (through all those if()\'s) you are gonna put a lot of the ACL-logic right in the middle of your application. Which is not the greatest thing to do in order to keep it simple and extendible. ;)</p>\n'}, {'answer_id': 26391, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 2, 'selected': False, 'text': "<p>I've used this in error reporting and it works quite well. As for user permissions it should work very well - you could have several columns for each user permission in your database or one userlevel column in your database. Go for this option.</p>\n"}]
['https://Stackoverflow.com/questions/17172', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/40/']