Multiplication Table

Multiplication TableThis Multiplication Table was created in PHP using new PHP commands namely, the complex “for” loop, and an increment operator that works like addition, but works in a way that it adds itself. For example we have a value of 1, if we increment the number, it would be 1+1, which is equals to 2. Using that in combination with the for loop, we could define how many increments it would undergo, let’s say, we tell PHP that it would be less than or equal to 12 (<=12), PHP will loop it 12 times, so it would start with 1 then add 1+1+1+1+1+1+1+1+1+1+1 into it. The final output would be 12.

While I manage to write this code and create a multiplication table, I have to confess that I wasn’t able to do it if wasn’t for the generous coders who share their PHP video tutorials on YouTube, kudos to you guys!

See Multiplication Table Demo Page

Code Talk

Code Talk

Creating a Multiplication Table in PHP requires only a short code, but it does require some serious analytical skills. For example, we do know that the Multiplication Table follows some sort of a pattern, but we also know that this spans both horizontally and vertically. And while creating a for loop in the horizontal axis can be easy, there are the next rows to consider.

Now, there are more than one way to achieve the table that we wanted to produce, one perhaps is to create 11 additional for loops for the numbers in the vertical axis, but that would require a long code, can be both time and resource consuming, and therefore deemed inefficient. Going back to the pattern that we mentioned before, it is important to note of this pattern. If in a multiplication table, horizontally the number increments by 1 and so does vertically, then it means we can do the same loop in the following rows as well. This is where we get the idea of looping the loop. Have a loop that takes care of the vertical numbers and loop it with another loop so it would do the process horizontally as well or vice versa.

Multiplication Table PatternI know, for loops are among the complex commands use in PHP so it might be difficult to understand. Anyway, just take a look at the picture to the right and you’ll get the idea of what I am trying to tell.

OK, going back to the idea of looping, while we can loop the first row of numbers in the next row in a similar pattern, all it would only do is copy the numbers above so if we have 1, 2, 3, 4 and 5 in the first row, then the next rows would also be 1, 2, 3, 4, and 5, which is not quite what we are expecting. However, do note that there is another pattern forming if numbers from both horizontal and vertical axis intersect in the middle. We are going to use this pattern in the next equation.

To make it simple, let’s call the horizontal axis as x, and the vertical axis as y. Since our aim here is multiplication, we can make x and y multiply so that the intersecting numbers will multiply with each other. This ultimately achieves our goal.

If we are to write this concept in PHP, this is how it would look like:

<?php
	echo "<table><tbody>";
	for($x = 1; $x <= 12; $x++) {
		echo "<tr>";
		for($y = 1; $y <=12; $y++) {
			echo "<td>" . $x * $y . "</td>";
		}
		echo "</tr>";
	}
	echo "</tbody></table>"
?>
Follow me at:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.