小编DJB*_*DJB的帖子

in_array vs strpos for php中的性能

我通过Windows身份验证登录用户,然后将该用户的权限存储在会话变量中.我在数据库中使用分隔的权限存储方法,即:

$rights //retrieved from database 
= 'read,edit,delete,admin'
Run Code Online (Sandbox Code Playgroud)

所以我的问题应该是我;

//generate variable
$_SESSION['userrights'] = $rights ($rights is retrieved from database)

//use strpos to find if right is allowed
if (strpos($_SESSION['userrights'],"admin") !== false) { // do the function }
Run Code Online (Sandbox Code Playgroud)

要么

//make array of rights
$_SESSION['userrights'] = explode(',',$rights)

//use in_array to find if right is allowed
if (in_array("admin",$_SESSION['userrights'])) { // do the function }
Run Code Online (Sandbox Code Playgroud)

OCD问题的一点,因为我认为差异对于我正在做的事情来说几乎可以忽略不计,但这会更快(使用更少的资源)方法?

除了侮辱我的权利存储方法之外,任何答案都会受到重视!

php arrays performance strpos

6
推荐指数
2
解决办法
4241
查看次数

标签 统计

arrays ×1

performance ×1

php ×1

strpos ×1