Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-16-2010, 09:00 PM Help understand code
Extreme Talker

Posts: 173
Trades: 0
Found this bit of code that is designed to cause the row color to alternate but I do not understand what $i&1 does. I understand that this is causing the class to alternate but I don't understand what that small piece of code is doing.


http://www.somacon.com/p338.php

PHP Code:
// PHP Version
$query "SELECT Adjective, Noun FROM DrSeussLines";
$result mysql_query($query);
$i 0;
print 
"<table>";
while((
$row mysql_fetch_row($result)) !== false) {
    
$i++;
    print 
"<tr class=\"d".($i 1)."\">";
    print 
"<td>".$row[0]."</td>";
    print 
"<td>".$row[1]."</td>";
    print 
"</tr>\n";
}
mysql_free_result($result);
print 
"</table>"
dgkindy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-16-2010, 09:15 PM Re: Help understand code
ThailandForum's Avatar
King Spam Talker

Posts: 1,415
Name: Sir Richard Cranium
Location: Bangkok Thailand
Trades: 0
telling it to goto the next color?
__________________

ThailandForum is offline
Reply With Quote
View Public Profile Visit ThailandForum's homepage!
 
Old 03-16-2010, 09:54 PM Re: Help understand code
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
& is a bitwise operator in PHP. It returns true if the bits of both operators are 1. It only takes into consideration the bits of the smallest operand. In this case it will only look at the least significant bit because the value 1 can be represented with a single bit. Here are some examples (all of the numbers are in binary):

10 & 1 will evaluate to 0.
11 & 1 will evaluate to 1.
101 & 10 will evaluate to 0.
11 & 10 will evaluate to 10.

Effectively, $i & 1 will return 1 when $i is odd and 0 when it is even. This is because all odd numbers end in a 1 in binary.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-17-2010, 06:31 AM Re: Help understand code
Extreme Talker

Posts: 173
Trades: 0
Thanks for the detailed explanation NullPointer.
dgkindy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help understand code
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13773 seconds with 12 queries