![]() |
php integer
How can I check if an integer is an even number?
I want to have a submit button and for each of the users that clicks it, it adds their usernames to a field in a table. However, I cannot have the # of users to be an uneven number (IE 1, 3, 5, 7 etc). So there is my problem. Anyone? |
I'm sure there are prolly cleaner ways to write this, but....
Code:
if($value % 2 == 0) |
you could do something like this... if you don't find a better way :)
if (($number/2) == (intval(($number+1)/2)) { //is even number } else { //is uneven number } |
Cheers people :)
I tried both of them first with a single figured number, and they both worked fine. I then tried with 20000000000000000000 just for the hell of it. Colins code said this was uneven, while Tektons said even. Then I tried with 20000000000000000001 which reversedly Colins code called uneven, while Tektons called it even. :p Now I dont need such a high number, just 100 or so. And they both say thats even XD Thanks again guys :) |
Quote:
PHP Code:
|
Also it's always nice to write a function for these things :)
PHP Code:
PHP Code:
|
Added the fucntion for future usage :)
But I feel pretty sure that the arguement in the function ($num in above) and the function coding ($number in above) should be equal ;) hehe |
Yep you're write, I've been programming all day so that's my excuse ;)
|
All times are GMT. The time now is 11:17 AM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|