douluan1533 2018-08-07 11:44
浏览 19
已采纳

数组在变量但使用条件时出错[关闭]

I am trying to condition if one of these has null value, then else but i am getting error:

ErrorException (E_NOTICE) Undefined variable: average_winning_trade

DD(winning_trades_profit) result : []

here is the code for condition:

 if(!empty($losing_trades_loss   && $profitable_trades  && $winning_trades_profit   && $losing_trades)) {
              $average_winning_trade = round(array_sum($winning_trades_profit) / $profitable_trades);
              $average_losing_trade = array_sum($losing_trades_loss) / $losing_trades;
              $payoff_ratio_per_trade = abs(round($average_winning_trade / $average_losing_trade, 2));
          }else  { $payoff_ratio_per_trade ="0";}
  • 写回答

2条回答 默认 最新

  • douchen2025 2018-08-07 12:15
    关注

    Your problem is in your if condition:

    if(!empty($losing_trades_loss   && $profitable_trades  && $winning_trades_profit   && $losing_trades))
    

    because $losing_trades_loss && $profitable_trades && $winning_trades_profit && $losing_trades is a boolean expression, it will always return true or false, and so empty() will always return false, so regardless of the state of those variables, the first part of your if statement will execute.

    I think what you actually want is

    if (isset($losing_trades_loss, $profitable_trades, $winning_trades_profit, $losing_trades))
    

    or possibly

    if (isset($losing_trades, $profitable_trades) && count($winning_trades_profit) && count($losing_trades_loss))
    

    without seeing the rest of your code it's difficult to say which of the above would be correct.

    Edit

    You probably also need to change your assignments to check for 0 divisors:

    $average_winning_trade = $profitable_trades ? round(array_sum($winning_trades_profit) / $profitable_trades) : 0;
    $average_losing_trade = $losing_trades ? array_sum($losing_trades_loss) / $losing_trades : 0;
    $payoff_ratio_per_trade = $average_losing_trade ? abs(round($average_winning_trade / $average_losing_trade, 2)) : 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?