Wednesday, May 30, 2007

alternator.php

/**
 * Guys, seriously. It is pretty common to want to do something where
 * you do something to every 2nd row, don't use a lengthy if else block.
 *
 * Just have one bool, $foo, and set it to !$foo on every iteration.
 * Simple!
 */
$i = 0;
$alt = true;

echo "<ul>";
while ($i < count($list))
{
    echo "<li";
    echo $alt ? " class=\"alt\"" : null;
    echo ">";

    echo htmlentities($list[$i]);

    echo "</li>";

    ++$i;

    $alt = !$alt; // aha! sorcery!
    /* if ($alt)
    {
        $alt = false;
    }
    else
    {
        $alt = true;
    } */
}
echo "</ul>";

No comments: