Reputation: 181
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";
}
Upvotes: 0
Views: 1055
Reputation: 6953
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
Upvotes: 3
Reputation: 1535
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
Upvotes: 1
Reputation: 10479
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
Upvotes: 1
Reputation: 7276
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";
}
Upvotes: 1