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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.