ameriblog Posted September 10, 2007 Share Posted September 10, 2007 I have a variable and I want to run an if clause to display one result if the variable is an odd number or display a different results if it is even. if ( $schedule_rs->Fields("team1") == 1 ) { echo "odd"; } else { echo "even"; } Quote Link to comment https://forums.phpfreaks.com/topic/68743-if-variable-is-an-odd-number/ Share on other sites More sharing options...
darkfreaks Posted September 10, 2007 Share Posted September 10, 2007 what exactly is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/68743-if-variable-is-an-odd-number/#findComment-345545 Share on other sites More sharing options...
wildteen88 Posted September 10, 2007 Share Posted September 10, 2007 Use the modulus (%) operator: if ( $schedule_rs->Fields("team1")%2 ) { echo "odd"; } else { echo "even"; } Quote Link to comment https://forums.phpfreaks.com/topic/68743-if-variable-is-an-odd-number/#findComment-345549 Share on other sites More sharing options...
dhimok Posted September 10, 2007 Share Posted September 10, 2007 try this if($var/2 == round($var/2)) echo "even"; else echo "odd"; Quote Link to comment https://forums.phpfreaks.com/topic/68743-if-variable-is-an-odd-number/#findComment-345551 Share on other sites More sharing options...
sasa Posted September 10, 2007 Share Posted September 10, 2007 or if($a & 1) echo 'odd'; else echo 'even'; Quote Link to comment https://forums.phpfreaks.com/topic/68743-if-variable-is-an-odd-number/#findComment-345554 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.