Write a PHP script that generates and displays a simple multiplication table for the numbers 1 to 5.

<?php
echo "<h2>Multiplication Table</h2>";
echo "<table border='1'>";
echo "<tr><th></th>";

// Display table headers (1 to 5)
for ($i = 1; $i <= 5; $i++) {
    echo "<th>$i</th>";
}
echo "</tr>";

// Generate and display the multiplication table
for ($i = 1; $i <= 5; $i++) {
    echo "<tr><th>$i</th>";

    for ($j = 1; $j <= 5; $j++) {
        $result = $i * $j;
        echo "<td>$result</td>";
    }

    echo "</tr>";
}

echo "</table>";
?>

 

Post your Answer