練習課題(1)



PHPの基礎



九九表をPHPで作りなさい

  • for文のネスト
  • tableをfor文で生成する場合は、「行」を基準に設定する



ソースコードPHP

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>九九表</title>

<style>
table {
  border-collapse: collapse;
  }

th, td {
  width: 50px;
  text-align: center;
  }

th {
  background: #ccc;
}

</style>

</head>

<h1>九九表</h1>

<table border="1">
<tr>
<th></th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th>
</tr>

<?php
for ($i = 1; $i <= 9; $i++) {
 print ('<tr>');
 print ('<th>' . $i . '</th>');

 for ($j = 1; $j <= 9; $j++) {
 print ('<td>' . $i * $j . '</td>');
}
print ('</tr>' . "\n");
}
?>

</table>

<body>
</body>
</html>

eclipseの内部ブラウザで表示【 chrome