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

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";}

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;

As your error code shows

ErrorException (E_NOTICE) Undefined variable: average_winning_trade

Meaning you are trying to call the variable somewhere else in your code while it hasn't been set yet. This could be either before or after you reach the if-statement you put here. However, this if-statement is not causing the error to appear.

UPDATE

Your current if-statement is as follow:

IF NOT EMPTY $losing_trades_loss AND $profitable_trades AND $Winning_trades

but you clearly want

IF NOT EMPTY $losing_trades_loss OR $poritable_trades OR $winning_trades

so update your if-statement as follow:

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";}