首页 > 解决方案 > How can i give negative value for variable?

问题描述

I am trying to make some function but, i am getting white page result when i put -ve value for $variable. Look this variable has -ve value : $long_total_profit_loss = "-900";

$long_total_profit_loss = "-900";
$short_sell_total_profit_loss = "-600";

//CONCLUTION
if(($long_total_profit_loss > $short_sell_total_profit_loss) && ($long_total_profit_loss>0)){

    echo "long has higher in profit";
}elseif (($long_total_profit_loss > $short_sell_total_profit_loss) && ($long_total_profit_loss < 0)){

    echo "long has higher loss";
}elseif (($long_total_profit_loss < $short_sell_total_profit_loss) && ($long_total_profit_loss > 0)){

    echo "short has higher in profit";
}
elseif (($long_total_profit_loss > $short_sell_total_profit_loss) && ($long_total_profit_loss < 0)){

    echo "short has higher loss";
}

标签: php

解决方案


我建议您以不同的方式构建您的条件(使用嵌套的 if 语句),这将为您提供更好的概览和可读性,因此您不会忘记特殊情况。

$long_total_profit_loss = -900;
$short_sell_total_profit_loss = -600;

if($long_total_profit_loss > $short_sell_total_profit_loss) {
   if($long_total_profit_loss > 0) {
     echo "long has higher in profit";
   }
   else { // $long_total_profit_loss<0 or =0
     echo "short has higher loss";
   }
}
else { // $long_total_profit_loss < $short_sell_total_profit_loss or equal
   if($short_sell_total_profit_loss > 0) {
      echo "short has higher in profit";
   }
   else {  // $short_sell_total_profit_loss<0 or =0
      echo "long has higher loss";
   }
}

// output: short has higher loss

您可以/应该扩展它以捕获这些值彼此相等且等于 0 的情况。

编辑
交换了“空头损失更高”和“多头损失更高”的措辞,这更有意义,感谢@Rafael


推荐阅读