Display Multiplication Table in PHP
What you’ll learn
- What people mean by a multiplication table (times table) and how it maps to a short
forloop. - A clear
echopattern for each row:base × i = product. - Two programs: fixed table for 5 and user-picked number with CLI input, plus a live preview.
Overview
You pick one number (the “base”). For each step i from 1 up to 10, print base × i. That is the whole program idea - no arrays needed for the basic version.
Two programs
Table of 5 baked in, then the same idea with typed input.
Live preview
Try another base and see lines n × 1 … n × 10 in the page.
Interview hook
Mention O(k) for k printed rows and optional input validation.
Prerequisites
for loops, echo, and (for example 2) basic CLI input.
- Basic PHP syntax and writing reusable functions.
- Comfort with integer multiplication
*in PHP.
The idea
Fix a base number n. Let i walk through 1, 2, 3, … up to your last row (often 10). Each line prints one product n * i. The loop variable i is the only thing that changes from line to line.
If you can say “5 times 7 is 35,” you already understand the math - the code just repeats that pattern automatically.
Live preview
Default base 5 matches example 1. Change it to any reasonable integer; rows run from 1 to 10.
Algorithm
Goal: print base × i for each i from 1 to last (here last = 10).
Choose base and range
Set base (fixed or from input). Set last (10 in these examples).
Print a header (optional)
A friendly title line helps when you compare output to a textbook table.
Loop
For i = 1 to last, print base, i, and base * i.
📜 Pseudocode
procedure print_table(base, last):
print header with base
for i from 1 to last:
print base, i, and base * iTable for 5 (fixed base)
Same spirit as the classic exercise: print 5×1 through 5×10. Uses a small reusable PHP 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);
?>Explanation
last controls how many rows you print - change it to 12 if your teacher wants a “1 through 12” table. The letter x in the output is just text; it is not the variable x from algebra.
Table for a number you type
Reads one integer and prints 1–10 for that base. Rejects non-positive values so the table stays in the usual “times table” style.
<?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);
?>Notes
Formatting. You can align columns with fixed widths (for example %3d) if you want a tidier grid for larger bases.
Larger numbers. PHP integers are usually enough for interview-style tables. For very large values, always test on your runtime and platform.
❓ FAQ
🔄 Input / output
Example 1 needs no input. Example 2 reads one integer from standard input with fgets(STDIN), then validates before printing the table.
Edge cases
Invalid CLI input
Validate parsed input and show a friendly message instead of printing an invalid table.
Non-positive base
Decide whether to reject (as here) or allow - classic tables usually use a positive base.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Print last rows | O(last) | O(1) |
Summary
- Core loop: for each
i, printbase * i. - Cost: linear in the number of rows printed.
- Stretch: read
base(and maybelast) from the user with validation.
A 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.
8 people found this page helpful
