Welcome to WebmasterWorld Guest from 54.161.110.186

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)

WebmasterWorld Senior Member 10+ Year Member



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 Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

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

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

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



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;
}

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

10+ Year Member



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

WebmasterWorld Senior Member 10+ Year Member



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 Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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 Jun 17, 2004 (gmt 0)

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



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

WebmasterWorld Senior Member 10+ Year Member



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 Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

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

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

WebmasterWorld Senior Member 10+ Year Member



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

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



> 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 Jun 18, 2004 (gmt 0)

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



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 Jun 18, 2004 (gmt 0)

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



>>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 Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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 Jun 18, 2004 (gmt 0)

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



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

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




>>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 Jun 18, 2004 (gmt 0)

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



>>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 Jun 18, 2004 (gmt 0)

10+ Year Member




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 Jun 18, 2004 (gmt 0)

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



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

10+ Year Member



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 Jun 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month