Hello students and dear programmers. Today’s script we are going to see how to generate a multiplication table using PHP script.
1 2 3 4 5 6 7 8 9 10 |
<?php $table_no = 7; $upto = 10; for($i=1; $i<=$upto; ++$i){ echo "$table_no * $i = ".$table_no * $i ."<br />"; } ?> |
The above script is so simple and the output will be something like this.
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
Now this is static how to make this one dynamic?. We need to write a HTML form to achieve this!.
Let’s see the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<!DOCTYPE html> <html> <head> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>PHP script to Generate Multiplication table!</title> </head> <body> <div class="container"> <div class="row"> <h2>Generate Multiplication table using PHP Script!</h2> <hr /> <div class="form-group"> <form class="form-horizontal" method="post"> <div class="form-group"> <label for="table_no" class="col-sm-2 control-label">Multiplication table of:</label> <div class="col-sm-2"> <input type="number" class="form-control" min="1" name="table_no" placeholder="Table no." required> </div> </div> <div class="form-group"> <label for="upto" class="col-sm-2 control-label">Upto:</label> <div class="col-sm-2"> <input type="number" class="form-control" min="1" name="upto" placeholder="Table upto" required> </div> </div> <div class="form-group"> <label for="upto" class="col-sm-2 control-label"></label> <div class="col-sm-2"> <input type="submit" class="form-control btn btn-primary" /> </div> </div> </form> </div> </div> <?php if(isset($_POST['table_no']) && $_POST['table_no'] != ''){ echo "<h3>Result:</h3>"; $table_no = $_POST['table_no']; $upto = $_POST['upto']; for($i=1; $i<=$upto; ++$i){ echo "$table_no * $i = ".$table_no * $i ."<br />"; } } ?> </div> </body> </html> |
You can see the demo of this script here: