首先,我不明白为什么有些人使用二进制数和 2 的幂来获得权限。是因为它们代表真/假,从后向关系为 1/0?
例如:1010 means false, true, false, true
?
我的问题如下。15
十进制与1111
二进制相同,但是:
DEFINE ('READ',1);
DEFINE ('WRITE',2);
DEFINE ('EDIT',4);
DEFINE ('DELETE',8);
$current = 15;
if ($current & READ) print("can read <br />");
if ($current & WRITE) print("can write <br />");
if ($current & EDIT) print("can edit <br />");
if ($current & DELETE) print("can delete <br />");
正如我所料,这输出允许所有权限。但是,如果我将 15 更改为,1111
则它只显示其中 3 个不允许全部四个(delete
权限无效):
DEFINE ('READ',1);
DEFINE ('WRITE',2);
DEFINE ('EDIT',4);
DEFINE ('DELETE',8);
$current = 1111;
if ($current & READ) print("can read <br />");
if ($current & WRITE) print("can write <br />");
if ($current & EDIT) print("can edit <br />");
if ($current & DELETE) print("can delete <br />");
是不是很奇怪?我理解错了吗?我需要一些帮助来理解这一点。我应该在数据库中将权限存储为小数还是二进制?