May 7 / ramol

How to make a integer variable switch between 0 and 1 every time a method is invoked?

It’s very simple. I’m using it very often.

$offset = 0;
for($i = 0; $i<10; $i++) {
  $offset = 1 - $offset;
  echo $offset;
}

The above example will output:
1010101010

One Comment

leave a comment
  1. Flaboy / Jun 21 2012

    The alternative is dividing by modulo 2

    PHP
    for($i=0; $i<10; $i++) {
    echo $i%2;
    }

    AS
    for(var i:Number=0; i<10; i++) {
    trace(i%2)
    }

Leave a Comment