Times Table
Base × i
Print one product per row for a chosen base number.
A multiplication table for base n is the list of products n×1 through n×k. This tutorial covers the for-loop pattern, CLI input with validation, a live preview, worked PHP examples, edge cases, and complexity.
Base × i
Print one product per row for a chosen base number.
i = 1..k
Repeat the same formula while only the counter changes.
Table of 5
Hard-code a base for a classic homework demo.
Validate
Read a positive integer, then print its table.
Any base
Try another base and see rows 1 through 10 instantly.
O(1) space
Printing k rows is linear in the row count.
A multiplication table (times table) for a base n lists the products n×1, n×2, … up to some last multiplier. School-style programs usually stop at 10.
In PHP you fix a base, loop a counter $i from 1 to $last, and echo each row as $base x $i = product. No arrays are required for the basic version.
It is a classic first loop drill: counters, multiplication, formatted output, and optional input validation in one short exercise.
Each row is $base * $i.
$last controls how many rows you print.
Hard-code a base or read it from STDIN.
Reject non-positive bases for school-style tables.
In short: choose a base, loop $i from 1 to $last, and print $base x $i = $base * $i on each line.
Given a base integer and a last multiplier (often 10), print each product from 1 through that last value.
// base = 5, last = 10
// 5 x 1 = 5
// 5 x 2 = 10
// ...
// 5 x 10 = 50 | Item | Type | Description |
|---|---|---|
$base | int | The number whose times table you print (usually > 0). |
$last | int | Highest multiplier (commonly 10 or 12). |
| Printed output | text | One line per product, optionally with a header. |
procedure print_table(base, last):
print header with base
for i from 1 to last:
print base, i, and base * i | Method | Idea | Notes |
|---|---|---|
for loop | Counter 1..last | Interview / homework default |
while loop | Manual counter | Same math; more boilerplate |
| Hard-coded echoes | Ten print lines | Works but does not scale |
| Goal | Pattern |
|---|---|
| Row product | $base * $i |
| Print a row | echo "$base x $i = " . ($base * $i) . "\n"; |
| Loop range | for ($i = 1; $i <= $last; $i++) |
| Read CLI input | $raw = trim(fgets(STDIN)); |
| Validate positive | if (!is_numeric($raw) || (int)$raw <= 0) |
| Custom length | Change $last (e.g. 12) |
Same table — different ways to get there.
1..lastThis page — clear when the count is known
$i++; whileSame products; you manage the counter yourself
10 echoesFine for demos; painful to change the range
validate inputSay what you do for 0 / negative / non-numeric
Reach for a times-table drill when loops and formatted output matter.
Teach counters without needing arrays or recursion.
Read a number, validate it, then print a table.
Quick check of loops, formatting, and edge talk.
A single-base table is the step before an m×n grid.
Printing millions of rows is I/O-bound — keep demos small.
Key benefit: one visual loop problem that covers counters, multiplication, output formatting, and input checks.
Default base 5 matches Example 1. Change it to any reasonable integer; rows run from 1 to 10.
Three complete PHP programs — fixed table for 5, CLI input with validation, and a custom 1–12 range. Click View Output to reveal sample console results.
A reusable helper and a hard-coded base of 5.
Print 5×1 through 5×10 with a small reusable function.
<?php
function printMultiplicationTable(int $base, int $last): void
{
echo "Multiplication table for $base:\n";
for ($i = 1; $i <= $last; $i++) {
echo "$base x $i = " . ($base * $i) . "\n";
}
}
$base = 5;
$last = 10;
printMultiplicationTable($base, $last);
?> $last controls how many rows you print — change it to 12 if you need a 1-through-12 table. The letter x in the output is just text; it is not an algebra variable.
Same loop; the base comes from STDIN with a positive-integer check.
Reads one integer and prints 1–10 for that base. Rejects non-positive values.
<?php
function printMultiplicationTable(int $base, int $last): void
{
echo "Multiplication table for $base:\n";
for ($i = 1; $i <= $last; $i++) {
echo "$base x $i = " . ($base * $i) . "\n";
}
}
echo "Enter a positive integer: ";
$raw = trim(fgets(STDIN));
if (!is_numeric($raw) || (int)$raw <= 0) {
echo "Please enter a positive integer.\n";
exit(1);
}
$n = (int)$raw;
printMultiplicationTable($n, 10);
?> fgets(STDIN) reads a line; trim removes the newline. The guard keeps the program in the usual “positive times table” style before the helper runs.
Same helper — pass a different $last for 1 through 12.
Shows that row count is a parameter, not a hard-coded magic number inside the loop.
<?php
function printMultiplicationTable(int $base, int $last): void
{
echo "Multiplication table for $base (1 to $last):\n";
for ($i = 1; $i <= $last; $i++) {
echo "$base x $i = " . ($base * $i) . "\n";
}
}
printMultiplicationTable(7, 12);
?> Keeping $base and $last as parameters makes the helper reusable for homework variants and interview follow-ups. The loop body never changes — only the bounds do.
Set $base (fixed or from input) and $last (often 10).
Print a title line so the table is easy to read.
For each $i from 1 to $last, print $base * $i.
All requested rows are printed.
5, First Five RowsTrace products for $base = 5 while $i runs from 1 to 5 (the full Example 1 continues to 10).
$i | Expression | Printed product |
|---|---|---|
1 | 5 * 1 | 5 |
2 | 5 * 2 | 10 |
3 | 5 * 3 | 15 |
4 | 5 * 4 | 20 |
5 | 5 * 5 | 25 |
Continue the same pattern through $i = 10 to finish Example 1.
Where a printed times table (and its loop pattern) shows up beyond the prompt.
Counters and repeated formulas without arrays.
Example: print table of 5.
Read, validate, then generate output.
Example: Example 2 pattern.
Align columns with padded widths later.
Example: sprintf-style fields.
Single-base tables lead to full m×n charts.
Example: outer base, inner multiplier.
State O(k) for k printed rows.
Example: last = 10 → 10 multiplies.
Reuse one helper with different bases and ranges.
Example: Example 3 pattern.
Pro Tip: open with “for i from 1 to last, print base × i” before writing any PHP syntax.
Why the looped times-table approach works well for beginners and interviews.
One formula, one counter — easy to dry-run on paper.
Change $base or $last without rewriting the loop body.
Add input and validation without changing the core idea.
O(k) time for k rows — easy to state in an interview.
Pro Tip: extract printMultiplicationTable($base, $last) early so fixed and interactive demos share one implementation.
Small habits that keep times-table solutions interview-ready.
Avoid magic 10 inside the loop when the prompt may ask for 12.
Check numeric and positive before printing.
Use $i <= $last so the final multiplier is included.
A short header makes sample output easy to grade.
One multiply and print per row.
Pro Tip: dry-run 5 x 1 through 5 x 5 aloud — if products match, your loop bounds are almost certainly correct.
Mistakes that commonly break multiplication-table solutions.
Using $i < $last skips the final row.
→ Prefer $i <= $last for inclusive ranges.
Printing a table for empty or non-numeric input.
→ Check is_numeric and positivity first.
Copy-pasted lines instead of a loop.
→ Use for so changing the range is one edit.
Including 0 when the assignment expects 1–10.
→ Confirm the prompt’s first multiplier.
x With MultiplicationThinking the printed x is an operator.
→ Compute with *; print x only as text.
Handle these before calling the table printer done.
Show a friendly message instead of printing an invalid table.
Decide whether to reject (as here) or allow — classic tables usually use a positive base.
Loop still works; output is just base x 1 = base.
PHP ints are usually fine for demos; formatting may need wider columns.
Validate $last if callers can pass zero or negatives.
trim input before casting to int.
Handy follow-ups interviewers sometimes ask.
n×k is n added to itself k times — the loop just prints the closed form.5×7 equals 7×5 — useful if you later print a full grid.for.Try these variations to lock in the pattern.
$last$i manually$i, print $base * $i.$base (and maybe $last) from the user with validation.Quick Takeaway: loop $i from 1 to $last, print $base * $i each time, and validate interactive input.
| Task | Time | Extra space |
|---|---|---|
Print $last rows | O(last) | O(1) |
| CLI parse + validate | O(1) | O(1) |
For interview-sized tables, the loop cost is tiny; clarity matters more than micro-optimizations.
A multiplication table is a counted loop: fix a base, walk $i from 1 to $last, and print each product. Parameterize the helper so fixed demos, CLI input, and custom ranges share one implementation.
Practice the three examples above, then continue to checking natural numbers.
for i = 1..last: print base × i — validate interactive bases before you loop.
for loop with inclusive $last< when you meant <=x as an operatorPrint times tables the interview-friendly way.
base × i
Mathi = 1..last
CodeHard-coded base
DemoValidate first
CLIO(k) / O(1)
AnalysisA multiplication table for a number n is just the list of products n×1, n×2, …. Each line is one multiplication — what you practiced as “times tables” in school — now printed by a loop instead of by hand.
Learn how to check whether a number is a natural number (positive integer) in PHP.
8 people found this page helpful