php - 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";
}
解决方案
我建议您以不同的方式构建您的条件(使用嵌套的 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
推荐阅读
- scala - Gatling 提取 xpath 值错误“尚未声明命名空间前缀‘soap’”
- javascript - 在唯一 ID 上合并两个数组
- reactjs - 在 React 路由器中无需重新渲染的 URL 更改
- xcode - 错误:无法生成进程(参数列表太长)
- json - 错误 json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)
- linux - 根据新版本的 config.yaml 文件,Linux 环境中 Iotedge 的非活动状态
- docker - 如何限制 docker 容器只监听来自 localhost 的连接?
- sql - 如何根据 30 分钟的时间间隔检索时间戳列的最小和最大时间?
- ios - swift deinit方法没有调用
- azure - Powershell AzureADPreview cmdlet Set-AzureADApplication“PreAuthorizedApplications”参数的正确语法是什么?