#1 (permalink)  
Old 09-02-2008, 06:38 PM
Registered User
 
Join Date: Jul 2004
Posts: 247
odd or even

How do I distinguish the odd and even in an if statement:
PHP Code:
for($i=0$i<count($spanId); $i++){
    if((
count($spanId[$i]) % 2) == 0){
        
$beginIllustratorNav "<span id = \"".$spanId[$i]."\" style=\"float: right; margin: 5px; display: block; width: ".$spanWidth[$i]."px; height: ".$spanHeight[$i]."px; border: 1px solid #F6F5E3;\" >";
        }else{
        
$beginIllustratorNav "<span id = \"".$spanId[$i]."\" style=\"float: left; margin: 5px; display: block; width: ".$spanWidth[$i]."px; height: ".$spanHeight[$i]."px; border: 1px solid #F6F5E3;\" >";
        }
...

__________________
Gilgal's website:
Wheel Of God
Now easy to post scripture verses!
Portfolio
Reply With Quote

  #2 (permalink)  
Old 09-03-2008, 06:44 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,438
Exactly the way you've written it.

The "if" portion of your statement is going to be even, the "else" portion of that statement is going to be odd.

Now, the way your statement is written, you are determining whether there are an odd or even number of items in the spanid[$i] array. I'm thinking you probably actually want to evaluate $i%2 to find out whether $i is even or odd, but I'm not sure.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #3 (permalink)  
Old 09-29-2008, 06:25 AM
Registered User
 
Join Date: Sep 2008
Posts: 7
Quote:
if((count($spanId[$i]) % 2) == 0)
I think it will ==1
e. g. 2/2=1
4/2=2 if no. is divide one time with 2 then it is even otherwise it is odd.
Reply With Quote
  #4 (permalink)  
Old 09-29-2008, 08:58 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,438
I don't think that's quite right. I'm not sure if you're understanding the modulus operator quite right.

The modulus operator divides number A by number B. Then, rather than returning the dividend (the result of A divided by B), it returns the remainder that's left over. Here are some samples with real numbers to help you understand:

1 % 1 = (1/1 = 1, exactly; there is no remainder, so the answer is going to be 0)
2 % 2 = (2/2 = 1, exactly; there is no remainder, so the answer is going to be 0)
3 % 2 = (3/2 = 1 with a remainder of 1, so the answer is going to be 1)
25 % 13 = (25/13 = 1 with a remainder of 12, so the answer is going to be 12)
47 % 12 = (47/3 = 3 with a remainder of 11, so the answer is going to be 11)

Therefore, to find even or odd, all you do is use the modulus operator to find the remainder when divided by 2. Here are some more examples:
1 % 2 = (1/2 = 0 with a remainder of 1, so that odd number will return 1 as its remainder)
2 % 2 = (2/2 = 1 with a remainder of 0, so that even number will return 0 as its remainder)
3 % 2 = (3/2 = 1 with a remainder of 1, so that odd number will return 1)
4 % 2 = (4/2 = 2 with no remainder, so it returns 0)
5 % 2 = (5/2 = 2 with remainder 1, so it returns 1)
6 % 2 = (6/2 = 3 with no remainder, returns 0)
7 % 2 = (7/2 = 3, remainder 1, returns 1), etc., etc., etc.

All even numbers will return 0 as their remainder; all odd numbers will return 1 as their remainder.

I'm not sure what 0 % 2 would return. I would imagine that it would throw a "divide by zero" error, but I'm not certain. Therefore, it's probably a good idea to check to make sure your number is not 0 (or empty) before testing its remainder. It's probably also a good idea to make sure that it "is_numeric".
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 04:45 AM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter