开发者

conditional statements as strings [duplicate]

This question already has answers here: Closed 11 years ago.

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($ente开发者_如何学Gor_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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜