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条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退