php - Alternate line colors - understanding a code provided -
$c = true; // let's not forget initialize our variables, shall we? foreach($posts $post) echo '<div'.(($c = !$c)?' class="odd"':'').">$post</div>"; i understand how works.
what trying example? alternate div row changing true false , false true?
yes.
$c = !$c assigns opposite value of $c itself. variable evaluated after assignment.
this results in changing value between true , false.
this codes takes advantage of foreach loop. if have normal for loop, use counter variable instead:
for($i = 0, $l = count($posts); $i < $l; $i++) { echo '<div'.(($i % 2)?' class="odd"':'').">{$posts[$i]}</div>"; }
Comments
Post a Comment