What are some best practices for using the modulo operator in PHP to style alternating rows?
When styling alternating rows in a table using PHP, the modulo operator (%) can be used to determine if a row is even or odd. By checking if the row index modulo 2 is equal to 0, we can apply different styles to even and odd rows.
<table>
<?php
for ($i = 0; $i < 10; $i++) {
if ($i % 2 == 0) {
echo '<tr style="background-color: #f0f0f0;">';
} else {
echo '<tr style="background-color: #ffffff;">';
}
echo '<td>Row ' . $i . '</td>';
echo '</tr>';
}
?>
</table>
Related Questions
- Are there any specific tutorials or resources available for filling a dropdown menu with values from a MySQL table in PHP?
- How can JSON be utilized in PHP to format and return data for jQuery consumption in an AJAX request?
- What are the advantages and disadvantages of using curl and PHP to generate XML files compared to other methods?