使用IF变量与比较运算符存储在变量中 [英] Using IF condtion with comparison operator stored in variable

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

问题描述

我有一套涉及比较运算符的规则。我想根据存储在规则中的比较运算符的值来执行一些任务。我按照以下方式进行操作,但不起作用。检查以下代码:

$ p $ if($ benRules [$ i] ['amountCriteria'] =='大于')
$ comparison ='>';
if($ benRules [$ i] ['amountCriteria'] =='等于')
$比较='==';
if($ benRules [$ i] ['amountCriteria'] =='Less than')
$ comparison ='<';
$ b $ if($ value ['1']。$ comparison。$ value [$ i] ['2']){
debug('Condtion checked');
}

问题总是检查条件为真。它将IF条件中的整个参数作为字符串,只要该字符串不为空即可执行括号内的代码。
请帮助我。

解决方案

它总是评估为真,因为您只是评估字符串的存在(你不能以这种方式在PHP中构造逻辑语句)。

为了实现你想要的,你需要使用switch语句(其中你只需要为每个潜在的比较运算符进行一对一的匹配)就像这样...
$ b $ pre $ switch($ comparison){
case'<':{if($ value ['1']< $ value [$ i] ['2']){...} break; }
case'==':{if($ value ['1'] == $ value [$ i] ['2']){...} break; }
case'>':{if($ value ['1']> $ value [$ i] ['2']){...} break; }
}

...或使用 eval 来解析逻辑。如果看起来像使用eval方法是不容易的,但是正如PHP手册所承认的那样:
$ b


eval()语言结构是非常危险,因为它允许
执行任意PHP代码。因此不鼓励它的使用。如果您
已仔细验证没有其他选择,而不是使用此
构造,请特别注意不要将任何用户提供的数据
传入其中,而无需事先进行正确验证。


因此,如果您不明白所涉及的风险,请坚持使用切换方法。也就是说,使用eval的基本解决方案将类似于以下内容:

  eval('return'。$ value ['1'] 。$ comparison。$ value [$ i] ['2']。';'); 


I have set of rules which involves comparison operators. I want to do some task based on the value of comparison operator stored in the rule. I am doing it in the following way but it is not working. Check the following code

if($benRules[$i]['amountCriteria']=='Greater than')
    $comparison='>';
if($benRules[$i]['amountCriteria']=='Equal to')
    $comparison='==';
if($benRules[$i]['amountCriteria']=='Less than')
    $comparison='<';

if($value['1'].$comparison.$value[$i]['2']){
    debug('Condtion checked');
}

problem is it always checks the condition to be true. it takes whole parameter inside IF condition to be string so as long as that string is not empty is executes the code inside the parenthesis. Please help me here.

解决方案

It always evaluates to true as you're simply evaluating the existence of a string (you can't construct logical statements in this manner in PHP).

To achieve what you're attempting, you'd need to either use a switch statement (where you simply have a one to one match for each potential comparison operator) like so...

switch ($comparison) {
    case '<': { if($value['1'] < $value[$i]['2']) { ... } break; }
    case '==': { if($value['1'] == $value[$i]['2']) { ... } break; }
    case '>': { if($value['1'] > $value[$i]['2']) { ... } break; }
}

...or use eval to parse the logic. If might seem like a no-brainer to use the eval approach, but as the PHP manual admits:

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

As such, if you don't understand the risks involved, please stick with the switch approach. That said, a basic solution using eval would resemble the following:

eval('return ' . $value['1'] . $comparison . $value[$i]['2'] . ';');

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

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