Here’s how to create multiplication tables using a PHP for loop :
PHP Code
<?php
$num = 5; // Change this to your desired number
for ($i = 1; $i <= 10; $i++) {
$result = $num * $i;
echo “$num x $i = $result <br>”;
}
?>
Explanation:
- We define a variable $num to hold the number for which you want the multiplication table.
- The for loop iterates 10 times ($i = 1; $i <= 10; $i++).
- Inside the loop, $result calculates the product of $num and the current loop counter $i.
- We echo a string displaying the multiplication operation and the result.
- <br> adds a line break after each multiplication.
Change $num to any number to generate its multiplication table. This code can be easily modified to create multiplication tables for a range of numbers.
Here’s an example of this :
Output