Print Table Using Do While , While & For Loop.
<?php /* Table Print */ $num = 1; $end = 10; $table = 5; /* Do While */ echo " <h3>Do While</h3> "; do{ $res = $table * $num; echo "$table * $num = $res </br>"; $num++; } While($num<=$end); //While Loop $num = 1; echo "<h3> While Loop </h3>"; while ($num <= $end) { $res = $table * $num; echo "$table * $num = $res </br>"; $num++; } //For Loop echo " <h3>For Loop </h3>"; for($num = 1; $num<=10;$num++){ $res = $table * $num; echo "$table * $num = $res </br>"; } // For Loop With Table echo "<table>"; echo "<thead><th>Table</th><th>Multiple</th><th>Number</th><th>Equal</th><th>Result</th></thead>"; echo "<tbody>"; for($num = 1; $num<=10;$num++){ $res = $table * $num; echo "<tr><td>$table</td><td> x </td><td>$num</td><td> = </td><td>$res</td></tr>"; //Row Printing } echo "</tbody></table>"; ?>
OUTPUT
Do While
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
While Loop
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
For Loop
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
For Loop With Table
Table | Multiple | Number | Equal | Result |
---|---|---|---|---|
15 | x | 1 | = | 15 |
15 | x | 2 | = | 30 |
15 | x | 3 | = | 45 |
15 | x | 4 | = | 60 |
15 | x | 5 | = | 75 |
15 | x | 6 | = | 90 |
15 | x | 7 | = | 105 |
15 | x | 8 | = | 120 |
15 | x | 9 | = | 135 |
15 | x | 10 | = | 150 |
DEMO
(Visited 289 times, 1 visits today)
Written by: