Php 如何给变量赋值为负值?
我试图创建一些函数,但是,当我为Php 如何给变量赋值为负值?,php,Php,我试图创建一些函数,但是,当我为$variable输入-ve值时,得到了白页结果。看这个变量有-ve值:$long\u total\u profit\u loss=“-900” $long\u利润总额\u亏损=“-900”; $short_sell_总利润_亏损=“-600”; //结论 如果($long\u总计利润\u亏损>$short\u sell\u总计利润\u亏损)&($long\u总计利润\u亏损>0)){ echo“长期利润较高”; }其他($long\u total\u profi
$variable
输入-ve值时,得到了白页结果。看这个变量有-ve值:$long\u total\u profit\u loss=“-900”代码>
$long\u利润总额\u亏损=“-900”;
$short_sell_总利润_亏损=“-600”;
//结论
如果($long\u总计利润\u亏损>$short\u sell\u总计利润\u亏损)&($long\u总计利润\u亏损>0)){
echo“长期利润较高”;
}其他($long\u total\u profit\u loss>$short\u sell\u total\u profit\u loss)和($long\u total\u profit\u loss<0)){
回声“长有更高的损耗”;
}其他($long_total_profit_loss<$short_sell_total_profit_loss)和($long_total_profit_loss>0)){
echo“空头利润较高”;
}
其他($long\u total\u profit\u loss>$short\u sell\u total\u profit\u loss)和($long\u total\u profit\u loss<0)){
回波“短路损耗较大”;
}
欢迎光临。
主要的问题是,这些语句的计算结果都不是真的。(所以你永远不会回应任何事情)
您错过了一个(-900小于-600):
elseif($long\u total\u损益<$short\u sell\u total\u损益)&($long\u total\u损益<0)){
回应“你的评价”;
}
正如@Dale在评论中指出的那样
使用字符串不是问题,php(正确的或错误的)将起作用
为您转换
我仍然建议将这些变量声明为数字。
最后一段代码应该是这样的:
<?php
$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 "your evalutation";
}
欢迎光临。
主要的问题是,这些语句的计算结果都不是真的。(所以你永远不会回应任何事情)
您错过了一个(-900小于-600):
elseif($long\u total\u损益<$short\u sell\u total\u损益)&($long\u total\u损益<0)){
回应“你的评价”;
}
正如@Dale在评论中指出的那样
使用字符串不是问题,php(正确的或错误的)将起作用
为您转换
我仍然建议将这些变量声明为数字。
最后一段代码应该是这样的:
<?php
$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 "your evalutation";
}
据我所知,您没有考虑到两种情况
<?php
$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";
} else {
echo "Whoops I didn't think about this one!";
}
据我所知,您没有考虑到两种情况
<?php
$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";
} else {
echo "Whoops I didn't think about this one!";
}
我建议您以不同的方式构造条件(使用嵌套的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
$long\利润总额\亏损=-900;
$short\u sell\u总利润\u亏损=-600;
if($多头\利润总额\亏损>$空头\卖出\利润总额\亏损){
如果($long\总利润\亏损>0){
echo“长期利润较高”;
}
其他{/$long\利润总额\亏损0){
echo“空头利润较高”;
}
否则{/$short\u sell\u total\u profit\u 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
$long\利润总额\亏损=-900;
$short\u sell\u总利润\u亏损=-600;
if($多头\利润总额\亏损>$空头\卖出\利润总额\亏损){
如果($long\总利润\亏损>0){
echo“长期利润较高”;
}
其他{/$long\利润总额\亏损0){
echo“空头利润较高”;
}
否则{/$short\u sell\u total\u profit\u loss我会写一个答案,因为我看到人们忽略了显示信息的要点
$long_total_profit_loss = -900;
$short_sell_total_profit_loss = -600;
在本例中,$long比$short小,这意味着如果您想知道:
- 如果$long的利润高于$short,那么$long必须大于$short(您的陈述是正确的):
if($long…>$short…&&&$long>0)echo'long的利润高于short
- 如果$long的损失高于$short,那么$long必须小于$short(您的陈述是不正确的)
如果($long…<$short…)echo“long”的损耗比short高
- 如果$short的利润高于$long,那么$short必须大于$long(您的陈述是正确的):
if($long…<$short…&&&$short>0)echo'short的利润高于long
- (最后)如果$short的损失高于$long,那么$short必须小于$long(您的陈述是正确的):
如果($long…>$short…)echo“short”的损耗比long高
这里最重要的是:
空头比空头亏损更高
相当于您代码中的空头比空头利润更高
两者的比较结果均小于零,因此队列中的最后一个原因无论如何都不会被触发。请求利润的原因的条件应大于零
最后一件事,你是对的,我相信你知道-900比-600小,但从你的第二份声明来看,情况并非如此。对不起。:)
[编辑]
@杰夫说得有道理,利润与大于零有关
[Edit2]
Jeff承认了代码中的错误,尽管他的解决方案具有最好的方法/可读性
我在这里修正了,希望他会更新他的答案,这样你就可以选择他的(不是我的)
$long\利润总额\亏损=-900;
$short\u sell\u总利润\u亏损=-600;
if($多头\利润总额\亏损>$空头\卖出\利润总额\亏损){
如果($long\总利润\亏损>0){
echo“长期利润较高”;
}其他{/$long\利润总额\亏损0){
echo“空头利润较高”;
}否则{/$long\u total\u profit\u loss我会写一个答案,因为我看到人们忽略了所显示信息的要点
$long_total_profit_loss = -900;
$short_sell_total_profit_loss = -600;
在本例中,$long比$short小,这意味着如果您想知道:
- 如果$long的利润高于$short,那么$long就有了