使用IF变量与比较运算符存储在变量中

编程入门 行业动态 更新时间:2024-10-16 02:31:36
本文介绍了使用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变量与比较运算符存储在变量中

本文发布于:2023-11-24 01:08:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623413.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   运算符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!