What is the role of bitwise AND and bitwise OR in the development process of PHP?
- /**
- * 1. Permission application
- * For which permissions you have, add up the values corresponding to these permissions
- * For example: if the moderator has permissions (add, delete, modify, query), then the moderator's permission value is stored as 15 (8 +4+2+1)
- * Then compare [sum of permission values] with [actual permission value] [located]
- * If the result is true, you have permission
- * If the result is false, you have no permission
- *
- * Note: permissions The value must be the Nth power of 2, starting from the 0th power. The 31st power is 2147483648
- * The 32nd power is 4294967296, which has exceeded the common int (10) maximum storage of 4294967295, so you must pay attention to the number of permissions (<31 )
- * Of course, if the storage format is bitint or varchar, which can store longer numbers, then the number of permissions can continue to increase
- */
- $permission = 15; //1+2+4+8 has all permissions
- $permissions = array(
- 8 => 'Add',
- 4 => 'Delete',
- 2 => 'Modify',
- 1 => 'Query'
- );
- foreach ($permissions as $key => $val) {
- if($key & $permission) {
- echo 'I have the power of ' . $val . '
';
- }
- }
Copy code
|