PHP Multiplication Table Program
Definition
What Is a Multiplication Table?
A multiplication table for a number n lists products n × 1, n × 2, n × 3, … up to a chosen limit (commonly 10).
For n = 5 (limit 10):
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
…
5 × 10 = 50
Pattern:
for i from 1 to limit:
print n × i = (n * i)
Approach
How to Print a Multiplication Table in PHP
Four clear steps — a simple for loop does the work.
- Choose the number
$n (hard-coded or from input). - Choose the upper limit (often
10). - Loop
$i from 1 to that limit. - Print each row as
$n × $i = $n * $i.
| Method | Idea | Time | Best for |
|---|
| Fixed 1–10 | for ($i = 1; $i <= 10; $i++) | O(1) rows | Classic beginner demos |
| Custom limit | Loop to a parameter $limit | O(limit) | Flexible / interview follow-ups |
Pseudocode
function PrintTable(n, limit):
for i from 1 to limit:
print n, "×", i, "=", n * i
Reference
Cheat Sheet
Keep these facts handy while writing the program.
| Topic | Detail |
|---|
| Row formula | $n * $i for $i = 1..limit |
| Default limit | 10 in many textbooks |
| Loop | for is the clearest choice |
| Output | echo "$n × $i = " . ($n * $i); |
| Console | echo one line per row |
| Classic check | 5 × 10 = 50 |
Try it
Live Preview
Enter a number to print its table from 1 to 10.
Trace
Worked Walkthrough — n = 5, limit 5
Each loop step multiplies n by the current i.
i = 1 → 5 × 1 = 5
i = 2 → 5 × 2 = 10
i = 3 → 5 × 3 = 15
i = 4 → 5 × 4 = 20
i = 5 → 5 × 5 = 25
Stop when i exceeds the limit
Code
PHP Programs
Start with the classic 1–10 table. Then use a custom limit when the problem asks for more (or fewer) rows.
Example 1 — Fixed 1–10 Table
Print n × 1 through n × 10 with a simple for loop.
<?php
$n = 5;
for ($i = 1; $i <= 10; $i++) {
echo $n . " × " . $i . " = " . ($n * $i) . "\n";
}
?>
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
How It Works
- Set
$n = 5 (or read it from input later). $i starts at 1 and runs while $i <= 10.- Each iteration echoes one row using
$n * $i. $i++ moves to the next multiplier.- After
$i = 10, the loop stops — ten rows printed.
Example 2 — Custom Limit
Accept $n and a row count so the table length is flexible.
<?php
function printTable($n, $limit) {
if ($limit < 1) {
throw new InvalidArgumentException("Limit must be at least 1.");
}
for ($i = 1; $i <= $limit; $i++) {
echo $n . " × " . $i . " = " . ($n * $i) . "\n";
}
}
printTable(7, 5);
?>
7 × 1 = 7
7 × 2 = 14
7 × 3 = 21
7 × 4 = 28
7 × 5 = 35
How It Works
- Pass both the number
$n and the upper $limit. - Reject
$limit < 1 so the loop has a sensible range. - The same
for pattern prints 1..$limit rows. - For
printTable(7, 5) you get five lines ending at 7 × 5 = 35. - Change the limit without rewriting the loop body.
Edge Cases & Pitfalls
Check these before calling the solution done.
Limit ≤ 0No rows
Validate the upper bound before looping.
n = 0All zeros
Every product is 0 — still a valid table.
Off-by-oneUse <= limit
$i < 10 stops at 9 and misses the last row.
InputCast carefully
Use (int) or validate with filter_var when reading CLI or form input.
Analysis
Time and Space Complexity
| Program | Time | Extra space |
|---|
| Fixed 1–10 | O(1) (10 rows) | O(1) |
| Custom limit | O(limit) | O(1) |
Work grows with the number of rows printed. No extra arrays are required.
Remember
Key Takeaways
Pattern: loop $i and print $n * $i.
Default: many problems use limit 10.
Flexible: pass a custom limit when needed.
Test: 5 × 10 = 50 is the last row of the classic table.
One line: for each $i from 1 to limit, print $n × $i = $n * $i.
Frequently Asked Questions
For a number n, it is the list of products n×1, n×2, n×3, … up to a chosen limit (often 10).
A loop prints every row automatically. Writing each line by hand does not scale when the limit changes.
Beginner programs often print 1 through 10. Interviews may ask for a custom upper limit.
Yes mathematically. Products still work (for example 0×5 = 0, (−3)×4 = −12). Validate only if your problem requires positive n.
Printing k rows takes O(k) time and O(1) extra space.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful