a Multiplication Table Generator in PHP

Here’s a Multiplication Table Generator in PHP that allows the user to input a number and generate its multiplication table.

The code also includes an explanation of how it works.

Project Description

The multiplication table generator will:

Allow the user to input a number.
Generate and display the multiplication table for that number (e.g., from 1 to 10).

Complete Code

<?php
// Initialize variables
$result = "";
$error_message = "";

// Check if the form is submitted
if (isset($_POST['submit'])) {
    // Get the number from the form
    $number = $_POST['number'];

    // Validate that the input is a numeric value
    if (is_numeric($number)) {
        // Start generating the multiplication table
        $number = (int)$number; // Convert the input to an integer
        $result = "<h3>Multiplication Table for $number</h3><ul>"; // Start output

        // Loop through 1 to 10 (or any range you want)
        for ($i = 1; $i <= 10; $i++) {
            $result .= "<li>$number x $i = " . ($number * $i) . "</li>";
        }

        $result .= "</ul>"; // End output
    } else {
        // Display error message if the input is not numeric
        $error_message = "Please enter a valid number.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multiplication Table Generator</title>
</head>
<body>
    <h1>Multiplication Table Generator</h1>

    <!-- Display error message if any -->
    <?php if ($error_message): ?>
        <p style="color: red;"><?php echo $error_message; ?></p>
    <?php endif; ?>

    <!-- Form to input the number for generating the multiplication table -->
    <form method="post" action="">
        <label for="number">Enter a number:</label>
        <input type="text" name="number" required value="<?php echo isset($number) ? $number : ''; ?>">

        <br><br>

        <button type="submit" name="submit">Generate Table</button>
    </form>

    <!-- Display the result if available -->
    <?php if ($result): ?>
        <?php echo $result; ?>
    <?php endif; ?>
</body>
</html>

 

Explanation of the Code

1. Handling Form Submission and Input Validation

if (isset($_POST['submit'])) {
    $number = $_POST['number'];

    // Validate that the input is numeric
    if (is_numeric($number)) {
        $number = (int)$number;
    } else {
        $error_message = "Please enter a valid number.";
    }
}

 

When the form is submitted (i.e., the submit button is clicked), the code checks if the form has been submitted using isset($_POST[‘submit’]).
It retrieves the user input from the form using $_POST[‘number’].
The is_numeric() function checks whether the input is a valid numeric value. If it’s not numeric, an error message is displayed.

2. Generating the Multiplication Table

$result = "<h3>Multiplication Table for $number</h3><ul>";
for ($i = 1; $i <= 10; $i++) {
    $result .= "<li>$number x $i = " . ($number * $i) . "</li>";
}
$result .= "</ul>";

 

If the input is valid, the script starts generating the multiplication table for the number.
The table is created using a loop (for loop) that runs from 1 to 10 (you can change this range if needed).
For each iteration, the multiplication result is calculated and appended to the $result string.
The results are stored as a list (<ul> and <li>), which is then displayed on the page.

3. Displaying the Result

<?php if ($result): ?>
    <?php echo $result; ?>
<?php endif; ?>

 

If a valid result has been generated, it is displayed in the HTML as a formatted multiplication table. The result is shown as an unordered list (<ul>).

4. HTML Form for Input

<form method="post" action="">
    <label for="number">Enter a number:</label>
    <input type="text" name="number" required value="<?php echo isset($number) ? $number : ''; ?>">

    <br><br>

    <button type="submit" name="submit">Generate Table</button>
</form>

 

The HTML form allows the user to input the number for which they want to generate the multiplication table.
The form uses the POST method to submit the data to the same PHP script.
A text input field is provided for the user to enter the number, and a submit button triggers the form submission.

How the Multiplication Table Generator Works

User Input: The user inputs a number in the form and clicks the “Generate Table” button.
Form Submission: The form data is sent to the PHP script via POST.
Validation: The script checks if the input is a valid number. If the input is invalid, an error message is shown.
Table Generation: If the input is valid, the script generates a multiplication table for the input number by multiplying the number with values from 1 to 10 (or any other range you choose).
Result Display: The multiplication table is displayed on the webpage in an ordered list format.

Potential Improvements

Custom Range: Allow the user to specify the range of the multiplication table (e.g., from 1 to 12).
Styling: Use CSS to improve the appearance of the table.
Input Validation: Add client-side validation using JavaScript to ensure that the user inputs a number before submitting the form.
Multiple Tables: Allow the user to generate multiplication tables for multiple numbers at once.

This simple PHP project is a great way to practice form handling, input validation, loops, and string manipulation.

Related posts

A Palindrome Checker in PHP

A simple BMI Calculator in PHP

A simple Calculator in PHP with an explanation of the code