How To Create Multiplication Table In PHP
Print multiplication table in a specific format
In this section, we will learn more about for loop or you can say complex loops and print the multiplication table in the proper format.
For loop example
<?php for($i=2;$i<=10;$i++) { for($table_counter=1;$table_counter<=10;$table_counter++) { printf("%d * %d = %d ",$i,$table_counter,($i*$table_counter)); echo " "; echo "
"; if($table_counter==10) { echo "
"; } } } ?>
- Example
- Run
Explanation
It is the best example of nested loops and printf function (printf function is basically used for formatted output).
Print multiplication table
<?php for($i=1;$i<=10;$i++) { for($table_counter=2;$table_counter<=5;$table_counter++) { printf("%d",($i*$table_counter)); echo " "; if($table_counter==5) { echo "
"; } } } ?>
- Example
- Run
Explanation
In this example, we are printing multiplication table from 2 to 5 by using for loop and printf statement.