shergold Posted August 26, 2009 Share Posted August 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172012-solved-if-statement-help/ Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 if ($y <= 100) Quote Link to comment https://forums.phpfreaks.com/topic/172012-solved-if-statement-help/#findComment-906987 Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172012-solved-if-statement-help/#findComment-906993 Share on other sites More sharing options...
shergold Posted August 26, 2009 Author Share Posted August 26, 2009 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/172012-solved-if-statement-help/#findComment-906998 Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 Make sure that y is what you expect it to be. That if statement looks fine to me, assuming that you have correct values for $y. try echoing $y before the if statement and see what it is Quote Link to comment https://forums.phpfreaks.com/topic/172012-solved-if-statement-help/#findComment-907003 Share on other sites More sharing options...
shergold Posted August 26, 2009 Author Share Posted August 26, 2009 ah thankyou, yeah $y had a value of 0 . Shergold. Quote Link to comment https://forums.phpfreaks.com/topic/172012-solved-if-statement-help/#findComment-907024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.