Multiplication is define : that a Mathematically operation where one number can multiply to other number, which symbolic representation is like : a × b, a ⋅ b, a ∗ b, or ab, where a and b are positive integer. For example 2 x 5 = 10 means 2 + 2 + 2 + 2 +2 = 10. Here a = 2 and b = 5. Means 2 added itself till b. print table in PHP we must be do an exercise on this.
program to print table in PHP
<table align="center" border='1' width="100%">
<?php
// filename : multiplication_table.php
$num = 10;
for($i = 1; $i <= 10; $i++)
{
echo "<tr>";
for($j =1; $j <= $num; $j++)
{
$multiplication_table = ($i * $j);
echo "<td>$j x $i = $multiplication_table </td>";
}
echo "<tr/>";
}
?>
</table>
Let’s Understand the logic to print table in PHP
- Define HTML table, which will show table in formatting
- Define $num = 10, that mean, we are printing table till 10. as per your need you can increase this e.g. 12,15,20 …n
- Iterate outer loop till 10, because multiply will be up to 10. for ex. 2 X 1, 2 X 2, 2 X 3, …., 2 X 10.
- Print “<tr>” to start row of table inside of outer loop
- Iterate inner loop till $num because, inner loop will be run till, multiplication want to print. e.g. $num = 10; mean multiplcation will be print from 1 to 10, if we assigned $num = 12 then multiplication will be print from 1 to 12.
- Inside inner loop multiplication of table printed in “<td>”, because table will loop in html format.
- finally close the </tr> outside of inner loop.
- finally close the “</table>” out side of PHP code. So that table is printing in HTML formats.