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"; } 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? 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"; } 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"; 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'; 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
Archived
This topic is now archived and is closed to further replies.