Write a php code to show the table of given number

PHP Programs

A table of a number can be printed using a loop in program.

Logic:

  • Define the number.
  • Run for loop.
  • Multiply the number with for loop output.

Example:

We'll print the table of 7.

Output:

For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share


PHP program to print table of a number:

The below program prints table of a number using a loop. The PHP echo statement is used to output the result on the screen.
Example

<!DOCTYPE html> <html> <body>   <?php $num = 9; for($i=1; $i<=10; $i++) { $product = $i*$num; echo "$num * $i = $product" ; echo '<br>'; } ?>   </body> </html>

Output

9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90

Please Share

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will see how to print the multiplication table of any given number using PHP. To make the multiplication table, first, we get a number input from the user and then use for loop to display the multiplication table.

    We use HTML and PHP to display the multiplication table. The HTML part is used to design a form to get the input from the user and the PHP part is used to execute the multiplication and display the results in table format.

    Example:

    PHP

    <!DOCTYPE html>

    <html>

    <body>

        <center>

            <h2 style="color: green;">

                GeeksforGeeks

            </h2>

            <h3>

                Program to print multiplication<br>

                table of any number in PHP

            </h3>

            <form method="POST">

                Enter a number: 

                <input type="text" name="number">

                <input type="Submit" 

                    value="Get Multiplication Table">

            </form>

        </center>

    </body>

    </html>

    <?php

    if($_POST) {

        $num = $_POST["number"];

        echo nl2br("<p style='text-align: center;'>

            Multiplication Table of $num: </p>

        ");

        for ($i = 1; $i <= 10; $i++) {

            echo ("<p style='text-align: center;'>$num"

                . " X " . "$i" . " = " 

                . $num * $i . "</p>

            ");

        }

    }

    ?>

    Output:


    How can I create table in php?

    The CREATE TABLE statement is used to create a table in MySQL..
    NOT NULL - Each row must contain a value for that column, null values are not allowed..
    DEFAULT value - Set a default value that is added when no other value is passed..

    How do you print a table in HTML and php?

    (1) If you create a new HTML page with just the invoice, the user can print that page using the browser's print capacity. (2) Alternatively, you can allow the user to download an invoice document which you create and print that. (3) You can use the JavaScript command window. print(); in conjunction with #1.

    How can I print 1 to 10 numbers in php?

    php $sum = 0; $str = ''; for($i = 1; $i<=10; $i++) { $sum = $sum + $i; $str . = $i == 10 ? $i." = " : $i." + "; } echo $str.

    Can we use table tag in php?

    When you display table data in a web browser from a MySQL database using a PHP application, it will be displayed unformatted. echo "</table>"; mysql_close($con);

    Chủ đề