Display Multiplication Table in PHP

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 2 Code Examples
Loops

What you’ll learn

  • What people mean by a multiplication table (times table) and how it maps to a short for loop.
  • A clear echo pattern 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.

Runs in your browser. Very large bases may wrap awkwardly in the box; your PHP program still computes the product.

Live result
Press “Print table”.

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

Pseudocode
procedure print_table(base, last):
    print header with base
    for i from 1 to last:
        print base, i, and base * i
1

Table for 5 (fixed base)

Same spirit as the classic exercise: print 5×1 through 5×10. Uses a small reusable PHP function.

php
<?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.

2

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
<?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

It is a neat list of answers for "n times 1," "n times 2," and so on - usually up to 10 in homework-style programs. Each row is one multiply.
The pattern repeats: same formula (base times counter), only the counter changes. A loop writes every row without copying the echo line ten times.
School tables often go 1-10. You can change the upper limit to 12 or any positive integer; it is just a design choice.
Yes. A for loop is convenient when you know how many steps you want; a while loop can do the same with a counter you update by hand.
Multiplication still works in PHP, but a "times table" for non-positive bases is unusual. Example 2 checks for positive input; you can tighten rules for your assignment.
Printing k rows costs O(k) time and O(1) extra space besides the output lines.

🔄 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

Bad input

Invalid CLI input

Validate parsed input and show a friendly message instead of printing an invalid table.

Zero / negative

Non-positive base

Decide whether to reject (as here) or allow - classic tables usually use a positive base.

⏱️ Time and space complexity

TaskTimeExtra space
Print last rowsO(last)O(1)

Summary

  • Core loop: for each i, print base * i.
  • Cost: linear in the number of rows printed.
  • Stretch: read base (and maybe last) from the user with validation.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful