How to make a print table of any number in PHP

PHP Program to create a print table of any number:


PHP program of multiplication print  table:

Hi, readers here we explain how we can program to create a print table of any number in PHP.

In this tutorial, first of all, we explain the logic behind this and then the full source code in a readable format.So you don’t have to bother anything but the prior knowledge of PHP operators and  for..loop construct will help you a lot.

The logic to create multiplication table:

Table of any number simply means that multiplying the number from 1 to 10.

2*1=2

2*2=4

2*3=6

2*10=20

Simple Example:

Here in this example, we create a form with one text field to enter the desired number to find multiplication table. Then store the number in the variable using PHP and run the for loop 10 times to print the multiplication table by using multiplication operator(*)  and echo statement to display the result.

Full Source Code:

<!Doctype html>
<html>
<head><title>Finding Table of any number using PHP</title>
</head>
<body>
<!--form is created using post method and submiting values itself -->

<form action="" method="post">
Enter a number:
<input type="text" name="number" />
<input type="submit" value="Table" />
</form>
<!--end of form-->

<!--php code starts-->
<?php
/*Php program to find table of any number*/

// if ($_POST) is true i.e.,checking that the HTML form is submited by the method="post" or not.
 if($_POST){
 
 /* collecting the value of input field using $_POST['parameter'] 
 and storing it in the variable. */

$num=$_POST['number'];
 
 echo "Table of $num: <br>";
 //using for loop to create multiplication table.
 
 for($i=1;$i<=10;$i++)
 { 
 $value=$num*$i; 
 
 echo "$num*$i=$value<br>";
 }
 
}

?>
<!--php code ends-->

</body>
</html>

 


OUTPUT of  print table of any number in PHP

print table


 

Leave a Comment