duanpie2834
duanpie2834
2018-08-22 10:29
浏览 31
已采纳

我怎样才能给变量赋值?

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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试制作一些函数但是,当我为 $变量。 看看这个变量有-ve值: $ long_total_profit_loss =“ - 900”;

  $ long_total_profit_loss =“ -  900”; 
 $ short_sell_total_profit_loss =  “-600”; 
 
 // CONCLUTION 
if(($ long_total_profit_loss&gt; $ short_sell_total_profit_loss)&amp;&amp;($ long_total_profit_loss&gt; 0)){
 
 echo“long的利润率更高”; 
  } elseif(($ long_total_profit_loss&gt; $ short_sell_total_profit_loss)&amp;&amp;($ long_total_profit_loss&lt; 0)){
 
 echo“long has higher loss”; 
} elseif(($ long_total_profit_loss&lt; $ short_sell_total_profit_loss)&amp;  ;&amp;($ long_total_profit_loss&gt; 0)){
 
 echo“short有更高的利润”; 
} 
elseif(($ long_total_profit_loss&gt; $ short_sell_total_profit_loss)&amp;&amp;($ long_total_profit_loss&lt; 0)  ){
 
 echo“短有更高的损失”; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duandeng2011
    duandeng2011 2018-08-22 10:52
    已采纳

    I'd recommend to structure your conditions differently (with nested if statements), which will give you a better overview and readability so you can't forget a special situation.

    $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
    

    You could/should extend this to capture the case when those values are equal to each other and equal to 0.

    EDIT
    swapped wording of "short has higher loss" and "long has higher loss", which makes more sense, thanks to @Rafael

    点赞 评论
  • douqiao1887
    douqiao1887 2018-08-22 10:44

    Welcome. The main problem is that none of that statements evaluate to true. (so you'll never echo anything) You missed one (-900 is smaller than -600) :

    elseif(($long_total_profit_loss < $short_sell_total_profit_loss) && ($long_total_profit_loss < 0)){
        echo "your evalutation";   
    }
    

    As @Dale pointed out in the comments

    use strings isn't the issue, php (rightly or wrongly) will do the conversion for you

    I still suggest to declare those variables as numbers.
    The final piece of code should be something like this:

    <?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";
    
            }
    
    点赞 评论
  • douxiong3245
    douxiong3245 2018-08-22 10:45

    From what I can gather, you haven't taken into account a couple of situations.

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

    I have only added an extra else to catch anything you haven't considered, I hope you can take this and run with it

    点赞 评论
  • douzhao1912
    douzhao1912 2018-08-22 11:44

    I'm gonna write an answer because I see people missing the point of the information displayed.

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

    In this case, the $long is a smaller number than $short, which means that if you want to know:

    • If $long would have a higher profit than $short, then $long has to be bigger than $short (this[your] statement is correct):

      if ($long... > $short... && $long > 0) echo 'Long has higher profit than short

    • If $long would have a higher loss than $short, then $long has to be smaller than $short (this[your] statement is incorrect):

      if ($long... < $short...) echo 'Long has higher loss than short

    • If $short would have a higher profit than $long, then $short has to be bigger than $long (this[your] statement is correct):

      if ($long... < $short... && $short > 0) echo 'Short has higher profit than long

    • (And finally) If $short would have a higher loss than $long, then $short has to be smaller than $long (this[your] statement is correct):

      if ($long... > $short...) echo 'Short has higher loss than long

    The main thing here is that:

    Short has higher loss than long is equivalent to Long has higher profit than short in your code.

    Both of them are comparing to smaller than zero so the last cause in the queue will never be triggered anyway. The one asking for a profit should have the condition bigger than zero.

    And the last thing, you're right, I'm sure you know that -900 is smaller than -600, but it didn't look like that looking at your second statement. Sorry. :)

    [Edit]

    @Jeff has a point, profit has everything to do with bigger than zero.

    [Edit2]

    Jeff mantained the mistake you have in your code, though his solution has the best approach/readability IMO.

    I fixed here, expecting he will update his answer so you can choose his (NOT MINE).

    $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 {  // $long_total_profit_loss<0 or =0
          echo "long has higher loss";
       }
    }
    

    Tested here: https://3v4l.org/PY0P2

    点赞 评论

相关推荐