0

可能重复:
在 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 但它没有用。

请帮助解决这个问题,这是非常必要的。非常感谢。

4

1 回答 1

0

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)

于 2011-04-15T06:20:54.600 回答