How to Alternate Row Colors (and More) using PHP

This is a quick and easy way to alternate chunks of code using PHP. This is useful if you’re alternating row colors in a table or chart using a standard ‘If/Else’ statement. ¬†Adjust as needed for your project!


//alternate row colors
if ($colorrow == '0') {
//output this chunk of code
$rowbgcolor = 'style="background-color: #ececec;"';
$colorrow = '1';
} else {
//otherwise output nothing
$rowbgcolor = '';
$colorrow = '0';
}
//output
echo '<tr '.$rowbgcolor.'>

Follow: