使用变量作为运算符 [英] Using a variable as an operator

查看:115
本文介绍了使用变量作为运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有如下内容:

$a = 3;
$b = 4;
$c = 5;
$d = 6;

我进行了类似的比较

if($a>$b || $c>$d) { echo 'yes'; };

一切正常.是否可以使用变量代替运算符? 像这样:

That all works just fine. Is it possible to use a variable in place of the operator? Something like:

$e = ||;

然后我可以将其用作

if($a>$b $e $c>$d) { echo 'yes'; };

推荐答案

否,该语法不可用.最好的办法是使用eval(),不建议使用它,特别是如果$ e来自用户输入(即表单)或每个操作员都以case为条件的switch语句

No, that syntax isn't available. The best you could do would be an eval(), which would not be recommended, especially if the $e came from user input (ie, a form), or a switch statement with each operator as a case

switch($e)
{
    case "||":
        if($a>$b || $c>$d)
            echo 'yes';
    break;
}

这篇关于使用变量作为运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆