I haven’t been doing that much work in php lately and now I’m stuck with something I just can’t figure out what could be the problem.
I just want to show a win if the result is positive. A lost if it’s negative and a tie if it’s equal.
But there is something wrong with my code that I tried to keep simple. Maybe there is another way to make it even more simple than this. But anyway. This one has something wrong. Can anyone see where I’m wrong?
I get a start value earlier and then I get a stop value.
If the direction is up and the stop value is higher that start value, then it should give me a Win.
If the direction is UP and the stop value is lower than the start value, then it’s a lost.
If the result is the same, then it’s a tie.
If the direction picked earlier is down then it should be the opposite.
I have tried to change bits a and pieces but now it’s just a complete mess in my head. I just can’t figure out what is wrong in this simple code.
I think this one is giving me the wrong result when I picked UP as direction.
if ($updown='UP') {
$result=($stop-$start);
if ($result > '0') {
$winner = 'WIN';
}
else if ($result < '0') {
$winner = 'LOST';
}
else if ($result == '0') {
$winner = 'TIE';
}
}
if ($updown='DOWN') {
$result =($start-$stop);
if ($result > '0') {
$winner = 'WIN';
}
if ($result < '0') {
$winner = 'LOST';
}
if ($result == '0') {
$winner = 'TIE';
}
}
It would help people understand your code if you used proper indentation. It is very difficult to follow your nested if statements as it currently reads.
That is what I tried to explain in my text above.
If updown = UP then I expect the stop to be higher than the start. That would be a WIN. But if the stop is lower than the start (when the direction was set to UP then that would be LOST. And if the start and stop are equal. Then it’s a TIE.
And the second one is to check the result if I picked DOWN. Then I just turned the math and check if the stop is lower than the start. Then the amount went down just like I expected and it would be a WIN. But if I picked down and the stop is higher than the start, then it would’ve been a LOST.
It’s a little like a game/gambling where I need to check the result.
The user is supposed to pick either UP or DOWN from the start value.
But I have tried all the math and if section so many times that I’m not sure what is wrong.
It is probably very, very, very easy to make this. But I just had too many things in my tired brain…
Because of the missing = sign when checking the value of $updown, you’re actually assigning those values. So it will always run through as if you started with a value of DOWN. You could always use switch() to deal with DOWN/UP instead, or if you are absolutely 100% sure that $updown is always UP or DOWN, just use else for the second part of the check - if it’s not “UP” then it must be “DOWN”.
Don’t forget to check that it is either UP or DOWN, and nothing else. A good habit to get into, even if your code on this occasion doesn’t actually do anything catastrophic when some joker calls it with ?updown=somerandomvalue