PCInk

msg:1284266 | 9:49 am on Jun 17, 2004 (gmt 0) |
Use the NOT operator (is it '!'?) if (!($year & 1)) {...
|
brotherhood of LAN

msg:1284267 | 10:10 am on Jun 17, 2004 (gmt 0) |
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

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

msg:1284269 | 10:21 am on Jun 17, 2004 (gmt 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!
|
PCInk

msg:1284270 | 11:41 am on Jun 17, 2004 (gmt 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!
|
jatar_k

msg:1284271 | 5:31 pm on Jun 17, 2004 (gmt 0) |
dcrombie's solution is what you are looking for Hester.
|
Hester

msg:1284272 | 8:15 am on Jun 18, 2004 (gmt 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.
|
timster

msg:1284273 | 12:40 pm on Jun 18, 2004 (gmt 0) |
Hope you check back, Hester: here's the code you need: if ($year % 2) { # It'd odd }
|
Hester

msg:1284274 | 12:51 pm on Jun 18, 2004 (gmt 0) |
But I want to test ONLY for an EVEN number.
|
brotherhood of LAN

msg:1284275 | 12:57 pm on Jun 18, 2004 (gmt 0) |
> 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)
|
httpwebwitch

msg:1284276 | 4:13 pm on Jun 18, 2004 (gmt 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
|
jatar_k

msg:1284277 | 5:50 pm on Jun 18, 2004 (gmt 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]
|
Timotheos

msg:1284278 | 7:01 pm on Jun 18, 2004 (gmt 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?
|
jatar_k

msg:1284279 | 7:04 pm on Jun 18, 2004 (gmt 0) |
whatever gets us all through the day ;)
|
httpwebwitch

msg:1284280 | 7:12 pm on Jun 18, 2004 (gmt 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"); } }
|
jatar_k

msg:1284281 | 7:16 pm on Jun 18, 2004 (gmt 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?
|
DigitalSorceress

msg:1284282 | 9:07 pm on Jun 18, 2004 (gmt 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*
|
jatar_k

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

msg:1284284 | 9:33 pm on Jun 18, 2004 (gmt 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))
|
Hester

msg:1284285 | 8:37 am on Jun 21, 2004 (gmt 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.
|
|