Source Code for /public/appendix-samples/times-tables-square.php

<h1>Times tables square</h1>
<p>
  This is a square of times tables, from 1 to 15.
</p>

<table>
  <tr>
    <th><!-- this cell intentionally left blank --></th>
    <?php
    // The top row of our table will have an empty column, then one column for each number from 1 to 15:
    for( $i = 1; $i <= 15; $i++ ) {
      echo '<th>' . $i . '</th>';
    }
    ?>
  </tr>

  <?php
  // We'll make 15 subsequent rows, using a variable called $row to count its number (1 to 15):
  for( $row = 1; $row <= 15; $row++ ) {
    // Each row number will start with a new row (<tr>) in the table:
    echo '<tr>';

    // The first cell in each row will be the number of the row:
    echo '<th>' . $row . '</th>';

    // Then we'll loop through 15 columns, using a variable called $col to count its number (1 to 15):
    for( $col = 1; $col <= 15; $col++ ) {
      // The value of the cell will be the product of the row and column numbers:
      echo '<td>' . ( $row * $col ) . '</td>';
    }

    // Each row will end with a new row (<tr>) in the table:
    echo '</tr>';
  }
  ?>
</table>