//使用for循环输出一个5行2列的HTML表格,同时将奇数行的背景色设置为绿色。(PHP).
<!DOCTYPE html>
<html>
<head>
<title>Feiyang Li's table!</title>
<style>
table{border: 1px solid black; width: 100%; border-collapse: collapse; text-align: center;}
td,th{border: 1px solid black;}
.green{background: green;}
.white{background: white;}
</style>
</head>
<body>
<?php
//使用for循环输出一个5行2列的HTML表格,同时将奇数行的背景色设置为绿色。
$row = 5;
$col = 2;
echo "<table>";
echo "<tr>";
for ($i = 1; $i <= $col; $i++) {
echo "<th>第 $i 列</th>";
}
echo "</tr>";
for ($i = 1; $i <= $row; $i++) {
$color = ($i % 2 == 0) ? 'white' : 'green';
echo "<tr class = $color>";
for ($j = 1; $j <= $col; $j++) {
echo "<td> $i </td>";
}
}
echo "</table>";
?>
</body>
</html>