Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 09-07-2004, 05:13 AM
Revan's Avatar
Revan Revan is offline
 
Join Date: Jan 2004
Location: Norway
Posts: 1,671
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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?
Reply With Quote
  #2  
Old 09-07-2004, 07:08 AM
Tekton Tekton is offline
 
Join Date: Jun 2004
Location: Wisconsin
Posts: 362
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 
  }
Reply With Quote
  #3  
Old 09-07-2004, 07:40 AM
Colin F's Avatar
Colin F Colin F is offline
 
Join Date: Jul 2004
Location: Switzerland
Posts: 1,551
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
}
Reply With Quote
  #4  
Old 09-07-2004, 02:33 PM
Revan's Avatar
Revan Revan is offline
 
Join Date: Jan 2004
Location: Norway
Posts: 1,671
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.


Now I dont need such a high number, just 100 or so. And they both say thats even XD
Thanks again guys
Reply With Quote
  #5  
Old 09-07-2004, 02:55 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.


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).
Reply With Quote
  #6  
Old 09-07-2004, 03:37 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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!';

Reply With Quote
  #7  
Old 09-07-2004, 05:35 PM
Revan's Avatar
Revan Revan is offline
 
Join Date: Jan 2004
Location: Norway
Posts: 1,671
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #8  
Old 09-07-2004, 05:45 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yep you're write, I've been programming all day so that's my excuse
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:08 PM.


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.04002 seconds
  • Memory Usage 2,237KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (3)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete