947740 Posted June 8, 2009 Share Posted June 8, 2009 How would I go about determining if a number is odd or even? Is there an easier way than this: if ($i % 2) { echo "$i is odd"; } else { echo "$i is even"; } Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/ Share on other sites More sharing options...
T0Rtur3 Posted June 8, 2009 Share Posted June 8, 2009 hmm didn't see that you changed your post. Not that I know of Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/#findComment-851788 Share on other sites More sharing options...
papaface Posted June 8, 2009 Share Posted June 8, 2009 $i = 10; if ( $i&1 ) { echo "$i is odd"; } else { echo "$i is even"; } Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/#findComment-851789 Share on other sites More sharing options...
947740 Posted June 8, 2009 Author Share Posted June 8, 2009 $i = 10; if ( $i&1 ) { echo "$i is odd"; } else { echo "$i is even"; } Nice. http://newsourcemedia.com/blog/php-how-to-find-odd-and-even-numbers/ Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/#findComment-851790 Share on other sites More sharing options...
papaface Posted June 8, 2009 Share Posted June 8, 2009 $i = 10; if ( $i&1 ) { echo "$i is odd"; } else { echo "$i is even"; } Nice. http://newsourcemedia.com/blog/php-how-to-find-odd-and-even-numbers/ Thanks for the help. If you used google in the first instance you would have saved everyones time... Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/#findComment-851792 Share on other sites More sharing options...
Maq Posted June 8, 2009 Share Posted June 8, 2009 The only other thing you can do is convert it into ternary to only make it one line. Other than that, there is no native function, if that's what you're asking. Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/#findComment-851793 Share on other sites More sharing options...
947740 Posted June 8, 2009 Author Share Posted June 8, 2009 The only other thing you can do is convert it into ternary to only make it one line. Other than that, there is no native function, if that's what you're asking. Okay. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/#findComment-851796 Share on other sites More sharing options...
PFMaBiSmAd Posted June 8, 2009 Share Posted June 8, 2009 It depends on what you mean by simpler and how you are using the logic in your code - echo ($i % 2) ? "$i is odd" : "$i is even"; echo ($i & 1) ? "$i is odd" : "$i is even"; Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/#findComment-851797 Share on other sites More sharing options...
Maq Posted June 8, 2009 Share Posted June 8, 2009 The only other thing you can do is convert it into ternary to only make it one line. Other than that, there is no native function, if that's what you're asking. Okay. Thanks. Sure, if you wanted to do this, then follow PFMaBiSmAd's post. Quote Link to comment https://forums.phpfreaks.com/topic/161414-solved-oddeven/#findComment-851799 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.