Welcome to WebmasterWorld Guest from 54.198.179.85

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Checking for even numbers

But using a check for odd ones first

     
9:46 am on Jun 17, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 5, 2002
posts:1318
votes: 0


Can anyone rewrite this script to check just for even numbers? At the moment it is checking for odd numbers, then using 'else' to do something when the number is not odd.

I have copied the start of the code from a comment in the online PHP manual. My script works great but obviously it is bad programming, as I'm doing nothing if the number is odd!

if ($year & 1) {} else {echo "</tr>";}

I've looked at operators in the manual but it doesn't say how some of them work in practice, and I am not big on maths.

Sorry to ask such a basic question. I am sure there is a simple answer.

9:49 am on June 17, 2004 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Aug 13, 2003
posts:1029
votes: 0


Use the NOT operator (is it '!'?)

if (!($year & 1)) {...

10:10 am on June 17, 2004 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4843
votes: 2


if($year & 1) {}

It's not doing anything because there's no code in the if block

for($i = 0;$i < 11;$i++)
{
if($i & 1) echo 1;
else echo 2;
}

dcrombie

10:13 am on June 17, 2004 (gmt 0)

Inactive Member
Account Expired

 
 


if ($year % 2 == 0) { 
// year is even
} else {
// year is odd
}
10:21 am on June 17, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 5, 2002
posts:1318
votes: 0


Most of the above suggestions retain the key problem with this script. I don't want PHP to do anything if the number is odd.

I tried the NOT operator but without the brackets. It did not work. Just tried it again as listed above and it worked!

Thank you PCink!

11:41 am on June 17, 2004 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Aug 13, 2003
posts:1029
votes: 0


No problem. The NOT operator is probably one of the most under-used facilities available to programmers. I must confess, I don't use it often!
5:31 pm on June 17, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


dcrombie's solution is what you are looking for Hester.
8:15 am on June 18, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 5, 2002
posts:1318
votes: 0


Not it is NOT. That still uses two statements - one is executed when the number is even, one when it is odd. But I only need one statement to run.

I consider this discussion closed. Thanks to everyone for their help.

12:40 pm on June 18, 2004 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 1, 2003
posts:815
votes: 0


Hope you check back, Hester: here's the code you need:

if ($year % 2) {
# It'd odd
}

12:51 pm on June 18, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 5, 2002
posts:1318
votes: 0


But I want to test ONLY for an EVEN number.
12:57 pm on June 18, 2004 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4843
votes: 2


> if (!($year & 1))

PCInk was on the ball I think, that code equates to "a number that is not odd" and only checks for that.

opposite of if($year & 1)

4:13 pm on June 18, 2004 (gmt 0)

Moderator from CA 

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4059
votes: 0


if (!$year%2){
// it's even, so do something.
}

the "%" is called a "MOD" operator. "a%b" returns the remainder of "a/b". if a is even, the remainder of a/2 is 0. If a is odd, a%2 it returns 1. thus the condition "!a%2" is true if a is an even number.

that little nugget of code is very good at doing table rows in alternating colours.

good luck

5:50 pm on June 18, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


>>Not it is NOT. That still uses two statements

dcrombie's works fine as posted just remove the else if you don't need it

if ($year % 2 == 0) {
// year is even
}

modulus [ca.php.net]

7:01 pm on June 18, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 13, 2003
posts:775
votes: 0


You guys are cracking me up on discussing such a simple concept for so long. Both ways work just fine. I'd say the bitwise operator is faster but at this level who cares?
7:04 pm on June 18, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


whatever gets us all through the day ;)
7:12 pm on June 18, 2004 (gmt 0)

Moderator from CA 

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4059
votes: 0



>>Not it is NOT. That still uses two statements
dcrombie's works fine as posted just remove the else if you don't need it
if ($year % 2 == 0) {
// year is even
}

Oh, I am appalled - the above has 3 lines in it, not one! (etc)

How about:


print($year%2?'odd':'even');

or you might try this gem:


function do_something_only_if_year_is_even($year){
$evenness_quotient=2;
$dividend=$year/$evenness_quotient;
if($dividend==floor($dividend)){
print("something");
}
}
7:16 pm on June 18, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


>>or you might try this gem:

how beautifully verbose ;)

make the 3 lines one then

if ($year % 2 == 0) echo "we're even";

:)

think Timotheos is laughing harder?

9:07 pm on June 18, 2004 (gmt 0)

New User

10+ Year Member

joined:June 16, 2004
posts:21
votes: 0



or you might try this gem:

function do_something_only_if_year_is_even($year){
$evenness_quotient=2;
$dividend=$year/$evenness_quotient;
if($dividend==floor($dividend)){
print("something");
}
}

Oh, I didn't realize the thread had switched to java

*ducking out of the way*

9:11 pm on June 18, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


I've got your jsp right here [webmasterworld.com] ;)
9:33 pm on June 18, 2004 (gmt 0)

New User

10+ Year Member

joined:June 16, 2004
posts:21
votes: 0


jatar_k:: LOL oh thank you - what a wonderful way to end my Friday!

(I'm in a constant battle with Java zealots who are always telling me how much better Java is than this or that (esp PHP and Perl which really annoys me) I get some small satisfaction at how much of our company is glued together by my Perl scripts (and bourne shell and PHP to a lesser extent))

8:37 am on June 21, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 5, 2002
posts:1318
votes: 0


dcrombie's works fine as posted just remove the else if you don't need it

Sorry, I got it wrong. I thought the even check was last so it had a redundant odd check first, but it was the other way round.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members