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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程