作为字符串的条件语句
可能的重复:
在 if 循环内回显
我试图编写一个交易系统,我有一个进入和退出列表策略。为了减少代码行数,我计划将每次进入和退出的所有策略放入一个数组中。我的数组是这样的,
$enter_strats = array(
array('name'=>"macd",'strat'=>"/$divergence[/$key]>0.1;"),
);
我在数组中包含条件语句,如上所述。 当我循环查看每日价格时,我必须检查每个入场策略是否正确。我的 if 语句是这样的
foreach($divergence as $key=>$value)
{
if($trade ==0)
{
foreach($enter_strats as $k =>$v)
{
$strat = $v['strat'];
$strat = str_replace("#","$",$strat);
eval("\$strat = \"$strat\";");
if ($strat)
{
$trade =1;
$book->save($key,$close[$key],$v['name']);
}
}
}
}
问题,因为它是一个字符串,它总是 if 总是将其评估为 true。我尝试在 if 中再放入一个 eval 但没有用。
请帮助解决这个问题,这是非常必要的。多谢。
Possible Duplicate:
echo inside if loop
I trying to code a trading system and i have a list of entry and exit strategies. To lessen the number of lines in the code I planned to put all my strategies into an array for each entry and exit. My array is like this
$enter_strats = array(
array('name'=>"macd",'strat'=>"/$divergence[/$key]>0.1;"),
);
I am including the conditional statements inside the array as above.
while I am looping thru everyday prices, I have to check for each entry strategy if they they are true. My if statement is like this
foreach($divergence as $key=>$value)
{
if($trade ==0)
{
foreach($enter_strats as $k =>$v)
{
$strat = $v['strat'];
$strat = str_replace("#","$",$strat);
eval("\$strat = \"$strat\";");
if ($strat)
{
$trade =1;
$book->save($key,$close[$key],$v['name']);
}
}
}
}
The problem since it is a string its always if is always evaluating it to true. I tried to put one more eval inside if but its of no use.
Please help to solve this problem, its is very essential. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为您试图减少代码中的行数。
数组旨在保存数据,而不是代码。
一旦你理解了它,你的代码就可以了。
“strat”应该只包含数据。例如,一个运算符和一个数字。
将变量名保留在字符串中是没有意义的。
特别是如果你已经有了这个变量。
您的代码中已经有 $divergence[$key] 。
因此,“strat”应该只是
array('>',0.1)
That's because you're trying to lessen the number of lines in the code.
Arrays intended to keep data, not code.
As soon as you understand it, your code will be okay.
'strat' should contain only data. An operator and a number for instance.
keeping variable name in the string makes no sense.
especially if you have this variable already.
You have already have $divergence[$key] in your code.
So, 'strat' should be just
array('>',0.1)