JJohnsenDK Posted December 18, 2007 Share Posted December 18, 2007 Hey Is it possible to declair even and uneven numbers in php? example: if($i != even number){ echo ""; } And by even and uneven number i mean, that 1, 3, 5, 7 and so on is uneven and 2, 4, 6, 8 and so on is even. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 If you can divide it by 2, it's even Quote Link to comment Share on other sites More sharing options...
chigley Posted December 18, 2007 Share Posted December 18, 2007 <?php function is_even($no) { if(is_int($no / 2)) { return true; } else { return false; } } if(is_even(25)) { echo "Even!"; } else { echo "Odd!"; } // Output: 'Odd!' ?> Untested, and off the top of my head. I hope it works Quote Link to comment Share on other sites More sharing options...
Barand Posted December 18, 2007 Share Posted December 18, 2007 use % operator to check the remainder when dividing by 2 <?php for ($i=1; $i <=10; $i++) echo $i, ' ', ($i % 2 == 0) ? 'Even' : 'Odd', '<br/>'; ?> Quote Link to comment Share on other sites More sharing options...
chigley Posted December 18, 2007 Share Posted December 18, 2007 A learning point for me here. What's it called with all this ? : stuff? I think it begins with a T? Need to read up on it Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 18, 2007 Share Posted December 18, 2007 you dont need a function for this. use the modulus operator % if($number % 2){ //odd number } modulus gives you the remainder. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 18, 2007 Share Posted December 18, 2007 A learning point for me here. What's it called with all this ? : stuff? I think it begins with a T? Need to read up on it Ternary operator value = condition ? value if condition true : value if condition false Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted December 18, 2007 Author Share Posted December 18, 2007 wow... many solutions ... thanks for all the answers Quote Link to comment Share on other sites More sharing options...
helraizer Posted December 18, 2007 Share Posted December 18, 2007 As has been said, this: <?php for ($i = 0;$i <= 30; $i++) { if($i % 2) { echo "$i is an odd number <br>"; } else { echo "$i is an even number <br>"; } } ?> will echo 0 is an even number 1 is an odd number 2 is an even number 3 is an odd number 4 is an even number 5 is an odd number 6 is an even number 7 is an odd number 8 is an even number 9 is an odd number 10 is an even number 11 is an odd number 12 is an even number 13 is an odd number 14 is an even number 15 is an odd number 16 is an even number 17 is an odd number 18 is an even number 19 is an odd number 20 is an even number 21 is an odd number 22 is an even number 23 is an odd number 24 is an even number 25 is an odd number 26 is an even number 27 is an odd number 28 is an even number 29 is an odd number 30 is an even number Sam Quote Link to comment 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.