php - Alternate line colors - understanding a code provided -


in example this:

$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

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -