For a current table, use CSS to stripe the data rows; if an assignment forbids CSS selectors, use PHP’s row counter and ask whether inline styles are allowed. These are different approaches: PHP can decide which row is even, but a class by itself cannot change its color without CSS.
Use CSS for a current implementation
Alternating table-row backgrounds are often called zebra striping. CSS keeps presentation separate from the PHP that generates the table. Put column headings in a <thead> and data rows in a <tbody>, then style only the body:
As an Amazon Associate I earn from qualifying purchases.
<table>
<thead>
<tr>
<th scope="col">Row</th>
<th scope="col">1</th>
<th scope="col">2</th>
</tr>
</thead>
<tbody>
<tr><th scope="row">1</th><td>1</td><td>2</td></tr>
<tr><th scope="row">2</th><td>2</td><td>4</td></tr>
</tbody>
</table>
tbody tr:nth-child(even) {
background-color: #eee;
}
even selects every second row, beginning with the second row in the body. Use odd instead if the first data row should receive the background. MDN’s table styling guide documents both patterns and also describes nth-of-type.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
If the PHP loop must choose the alternating rows
When the exercise requires the loop to decide row parity, use the row number and PHP’s remainder operator. For positive row numbers, a remainder of zero after division by two means the row is even; PHP documents this operator in its arithmetic operators reference.
#1 Best Overall
for ($row = 1; $row <= 12; $row++) {
$rowClass = ($row % 2 === 0) ? 'even' : 'odd';
echo '<tr class="' . $rowClass . '">';
// Output the row header and cells here.
echo '</tr>';
}
This marks rows but does not color them on its own. If CSS is permitted, define styles for .even and .odd. If the assignment bans CSS selectors, ask whether an inline style attribute is permitted; otherwise the class has no visual effect. The original SitePoint forum discussion does not establish which interpretation the instructor intended.
Keep the header out of the stripe pattern
Target rows inside tbody, not every table row, so column headings are not included in the alternating sequence. Include an explicit <tbody>: as MDN explains in its table row reference, browsers can insert one when rows are written directly under a table. A selector that expects table > tr may therefore fail to match the rendered structure.
Rank #2
Clarify the assignment’s “HTML attribute” requirement
The 2017 SitePoint question asks for alternating colors using “html attribute names” while also requiring valid HTML5 and disallowing CSS3 selectors and properties. Those terms do not point to one unambiguous method. Forum replies discuss possibilities such as bgcolor, inline style, or class, but do not confirm the instructor’s intent. Ask which attribute or technique is allowed before choosing an implementation: a class still needs a stylesheet, while inline styling is itself CSS.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Quick Recap
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




