【问题标题】:Evaluate value of string as a condition and execute IF statement评估字符串的值作为条件并执行 IF 语句
【发布时间】:2017-02-23 12:03:30
【问题描述】:

我希望我的标题不会令人困惑。我需要的是:

我在一个字符串中有一个条件:

$user_rights = "$USER[ADMIN] || $USER[RIGHT1] || $USER[RIGHT2]";

var_dump($user_rights)

string(11) "0 || 0 || 0"

我想像这样将变量 $user_rights 放入 IF 语句中

if($user_rights)

我希望它以同样的方式工作

if($USER[ADMIN] || $USER[RIGHT1] || $USER[RIGHT2])

有可能吗?

【问题讨论】:

  • (为什么)它必须是一个字符串?为什么不使用包含权限的数组,然后您可以简单地循环检查所需的权限是否在其中?

标签: php if-statement evaluation


【解决方案1】:

Tty 这个:-

$string = "$USER[ADMIN] || $USER[RIGHT1] || $USER[RIGHT2]";
//convert this to array
$array = explode("||",$string);

//if you know inside string only three variable
if($array[0] || $array[1] || $array[2] ){
    //your condition
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 2017-04-08
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多