PHP: Nested For Loops

Derek Bridge

Department of Computer Science,
University College Cork

PHP: Nested For Loops


Loops within loops

Example of a nested loop

for ( $i = 0; $i < 4; $i++ )
    for ( $j = 0; $j < 3; $j++ )
        echo "<p>i: {$i} j: {$j}</p>";

Class exercises

  1. Suppose mult.html contains this form:
    <form action="mult.php" method="get">
        <input type="text" name="number" />
        <input type="submit" />
    Write mult.php, which outputs (as an HTML table) a set of multiplication tables ('times tables') for the number that the user enters. E.g. if the user enters 12, then your output runs from '1 times 1 is 1' up to '12 times 12 is 144'
  2. Write a PHP script that outputs a 'diary' for the current year with one line per day