Newbie and error

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.

Sorry. I’m just playing around with this and not sure how to do it the right way…
So I’m not sure what you want me to do here.

It would also help if you gave sample test values and the results expected.

This isn’t going to work the way you want it to:

	if ($updown='UP') {
...
	if ($updown='DOWN') {

You need to have a double-equal sign if you’re testing for equivalence, as you do in other places.

1 Like

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…

Aha… that might be why. I will try to change that part and see if works.
I keep forgetting the double equal signs all the time…

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”.

Yes. I will get either UP or DOWN from the url with a GET.

If and only if you are running PHP 7, search for the “Spaceship Operator”.

Your code will be drastically reduced :slight_smile:

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

2 Likes

If and only if you are running PHP 7, search for the “Spaceship Operator”.

Your code will be drastically reduced

Online Demo - with original errors

Online Demo - PHP 7 SpaceShip Operator

Source supplied for both demos

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.