vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   php integer (https://vborg.vbsupport.ru/showthread.php?t=69172)

Revan 09-07-2004 05:13 AM

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?

Tekton 09-07-2004 07:08 AM

I'm sure there are prolly cleaner ways to write this, but....

Code:

if($value % 2 == 0)
  {
    // it's even
  }else{
    $value += 1;  // or whatever
  }


Colin F 09-07-2004 07:40 AM

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
}

Revan 09-07-2004 02:33 PM

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 :)

filburt1 09-07-2004 02:55 PM

Quote:

Originally Posted by Revan
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 :)

Tekton's method is by far more preferable (modulus division).

PHP Code:

if ($number == 0

...evaulates to true if the number is evenly divisible by 2 (i.e., even).

Dean C 09-07-2004 03:37 PM

Also it's always nice to write a function for these things :)

PHP Code:

function is_even($num)
{
        if(
$number == 0)
        {
                return 
true;
        }
        else
        {
                return 
false;
        }


Then to check if it's even use this:

PHP Code:

$number 2;
if(
is_even($number))
{
       echo 
'it\'s even!';
}
else
{
        echo 
'it\'s not even!';



Revan 09-07-2004 05:35 PM

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

Dean C 09-07-2004 05:45 PM

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
  • Page Generation 0.01008 seconds
  • Memory Usage 1,731KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (3)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete