Program for show multiplication table using php

In this tutorial , we are going to learn program for showing multiplication table using PHP

 

 

 



HTML:


<!DOCTYPE html>
<html>
<head>
    <title>Multiplication Table</title>
</head>
<body>
    <form method="POST">
        <label>Enter a number: </label>
        <input type="text" name="number">
        <button type="submit" name="submit">Submit</button>
    </form>
    <?php
        include 'multiplication.php';

        if(isset($_POST['submit'])){
            $number = $_POST['number'];
            $table = new MultiplicationTable($number);
            $table->displayTable();
        }
    ?>
</body>
</html>


PHP Code:



<?php
class MultiplicationTable {
    private $number;

    public function __construct($number) {
        $this->number = $number;
    }

    public function displayTable() {
        echo '<table>';
        for($i = 1; $i <= 10; $i++) {
            echo '<tr>';
            echo '<td>'.$this->number.'</td>';
            echo '<td>*</td>';
            echo '<td>'.$i.'</td>';
            echo '<td>=</td>';
            echo '<td>'.$this->number * $i.'</td>';
            echo '</tr>';
        }
        echo '</table>';
    }
}
?>


The HTML code contains a form that takes a number as input and a submit button. When the user submits the form, the PHP code is executed to create an instance of the MultiplicationTable class and call its displayTable method.

The MultiplicationTable class takes a number as its constructor argument and stores it in a private property $number. The displayTable method then generates an HTML table that displays the multiplication table of the number.

The for loop in the displayTable method iterates from 1 to 10 and generates a table row for each number. Inside each row, the number, multiplication symbol (*), iterator, equal sign (=), and the result of multiplication are displayed in separate table cells.

When the PHP code is executed, it includes the MultiplicationTable.php file and creates an instance of the MultiplicationTable class with the number entered by the user. It then calls the displayTable method to generate the multiplication table and display it on the HTML page.


Previous Post Next Post