Jump to content

[SOLVED] IF statement help


shergold

Recommended Posts

Hey guys, I was wondering what is wrong with the following code, it should be executing the code that executes when the if is true, but it is executing the else code. $x and $y variables both equal 0.

 

if (isset($_POST['up']))
{
	$y = $y + 10;

	if (!$y > 100)
		$moved = 1;
	else
		{
			$y = $y - 10;
			echo "You cant move any further forward as you have reached the boundry";
		}
}

 

Thanks,

shergold.

Link to comment
https://forums.phpfreaks.com/topic/172012-solved-if-statement-help/
Share on other sites

if (!$y > 100) doesn't work because (i'm not sure tough) ! and > share the same execute rights meaning that !$y is executed first and afterwards $y > 100 thus if $y contains a value > 0 then in the context of !$y will return false afterwards in the context of $y > 100 is $y a boolean used in a integer context boolean to integer returns 0 for false 1 for true thus 1 > 100 will return false if (false) executes else.

ok thanks for everyones help, im also having trouble with the following if statement doing the same as the previous:

 

elseif (isset($_POST['down']))
{
	$y = $y - 10;

	if ($y >= 0)
		$moved = 1;
	else	
		{
			$y = $y + 10;
			echo "You cant move any further south as you have reached the boundry";
		}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.