PHP Program to Print Multiplication Table

PHP

A multiplication table is a useful mathematical tool that displays the products of a given number with a range of values, typically from 1 to 10. In this article, we will learn multiple ways to generate and print multiplication tables in PHP.

Formula to Generate Multiplication Table

To create a multiplication table for any number, use the formula ?

Answer = Number × Multiplier

Where:

  • Number is the fixed value for which the table is generated
  • Multiplier is the range of values (e.g., 1 to 10)

Example 1

  • Input: Number = 5
  • Output:
    5 x 1 = 5
    5 x 2 = 10
    5 x 3 = 15
    5 x 4 = 20
    5 x 5 = 25
    5 x 6 = 30
    5 x 7 = 35
    5 x 8 = 40
    5 x 9 = 45
    5 x 10 = 50

Using a Direct Loop

This is the simplest method to print multiplication tables using a basic for loop. The program calculates each multiple of the given number step by step ?

<?php
// Define the number
$number = 5;

// Print the multiplication table
for ($i = 1; $i <= 10; $i++) {
    $result = $number * $i;
    echo "$number x $i = $result<br>";
}
?>
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Using a Function

Creating a reusable function makes it easy to generate multiplication tables for different numbers. This approach is useful when you need to calculate tables for multiple values ?

<?php
// Function to generate a multiplication table
function printMultiplicationTable($number) {
    for ($i = 1; $i <= 10; $i++) {
        $result = $number * $i;
        echo "$number x $i = $result<br>";
    }
}

// Call the function with a specific number
$number = 7;
printMultiplicationTable($number);
?>
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Using While Loop

You can also use a while loop for generating multiplication tables, providing an alternative approach ?

<?php
$number = 3;
$i = 1;

echo "Multiplication table for $number:<br>";
while ($i <= 10) {
    $result = $number * $i;
    echo "$number x $i = $result<br>";
    $i++;
}
?>
Multiplication table for 3:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Real-World Applications

  • Teaching basic arithmetic calculations in educational applications
  • Automating repetitive calculations in mathematical scripts
  • Generating numerical reports and data visualization in web applications

Conclusion

PHP provides multiple approaches to generate multiplication tables, from simple loops to reusable functions. The choice depends on your specific needs use direct loops for simple cases and functions for reusable code.

Updated on: 2026-03-15T10:44:52+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements