Programming Zebra Stripes

Recently, I needed to use a programming trick to make a for loop alternate every other time. This can be done using a little trick called “zebra striping.”

It’s simple, set a variable to zero before the loop, then within the loop run the variable through an equation of 1 – variable. This will effectively make the loop either a zero or one every time the loop is run. See example code below…


$alt = 0;

for($i = 0; $i < 20; $i++) {

	// do some stuff here

	$alt = 1 - $alt;


Just thought I’d share a helpful little snippet.

Leave a Reply

Your email address will not be published. Required fields are marked *