dousi4148 2011-04-15 06:11
浏览 25

条件语句作为字符串[重复]

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.

  • 写回答

1条回答 默认 最新

  • dongtanlin0765 2011-04-15 06:20
    关注

    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)

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算