PHP

Print Table In PHP

Printing a table in PHP is quite an easy task, you can print a table by using PHP loops, printing tables is a good practice for learning PHP loops.

In this tutorial, we are going to have a look at 3 simple ways for printing tables in PHP.

1) we will create a table for the user’s choice which means we will take user input and according to the user’s input table will be printed.

2) we will create a default table. We will give the default table number in PHP code.

3) we will see how to print tables inside a table from 1 to 10.

So let’s start with the first way.

Print table in PHP using user’s input

<form Method="post">
 <br><br>
 Enter table number:<input type="text" name="val"/><br><br>
 <input type="submit" name="sub-btn" value="submit" />
 
</form>
<?php
if(isset($_POST['sub-btn']))
{
  $num=$_POST['val'];
  $result=0;
  for($i=1;$i<=10;$i++)
  {
    $result=$num*$i;
    echo $num;
    echo"*";
    echo $i;
    echo "=";
    echo $result;
    echo "<br>";
  }
}
?>

Code Explanation

In the above code, we created an HTML form then we created 2 input fields, the first input field is for taking user input and the second one is for submit button.

Then we closed the form tag and started the PHP code, in the PHP code we used the if condition for checking if a user clicked the submit button or not.

After that we declared 2 variables “$num” and “$result”, in $num we stored the user’s input number and we assigned 0 to $result.

We used for loop for printing the table, the for loop will start printing the table from 1 and will stop on 10 by incrementing it.

Then we used the $result variable and assign the value which will come after multiplying the user’s value and loop value i.e if the users’ input value is 9 and its multiples by loop value mean 1 so it will become 9 and the loop will continue till the whole table printed.

In last we just printed the table in a table syntax using echo, check out the output below.

Input

Output

8*1=8
8*2=16
8*3=24
8*4=32
8*5=40
8*6=48
8*7=56
8*8=64
8*9=72
8*10=80

As you can see the output above, it printed the table by taking user’s input.

Print table in PHP by default value

<!DOCTYPE html>
<html>
<body>
 
<?php    
$number = 4;  
for($i=1; $i<=10; $i++)   
{
$table = $i*$number;
echo "$number * $i = $table" ;   
echo '<br>';   
}  
?> 
 
</body>
</html>

Code Explanation

In the above code, we write PHP code in HTML structure than in the PHP code we declared a variable “$number” in which we stored a value for the table.

We used for loop for printing table, then we declared a variable “$table” and assign it the multiplied value of $i(loop variable) and $number (default value stored).

In the end, we just printed the table in the table syntax using echo, check out the output below.

4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40

Print tables in PHP using nested loops

<table align="center" border='1' width="100%">
<?php
$number = 15;

for($i = 1; $i <= 10; $i++)
{ 
    echo "<tr>";
  
  for($j =1; $j <= $number; $j++)
  {
    $table_multiply = ($i * $j);
    echo "<td>$j  x $i =  $table_multiply </td>";
  }
  
  echo "<tr/>";
}
?>
</table>

Code Explanation

In the above code we created an Html table then started the PHP code and declared a variable in which we stored the number of the table we want to be printed. Like we gave it 15 that means 15 tables will be printed.

Then we created an outer loop that will perform the task of printing the table till 10, for example, 1×1, 1x=2…, 1×10.

Then we printed <tr> table row.

After that, we created an inner loop that will perform the task of printing and multiplication tables.

Basically, the inner loop will print the table according to the number we stored in $number variable which means the inner loop will print 15 tables and will also perform the multiplication of these 15 tables.

In the inner loop, we declared a variable “table_multiply” and assigned it the multiplied value of $i(outer loop variable) and $j(inner loop variable).

In the end, we just printed the tables in the table syntax as <td> (table data) using echo, check out the output below.

Output

Conclusion

In this tutorial, we discussed 3 ways of printing a table in PHP, in the first way we printed a table by user input, then in the second way we printed a table by default value.

Last, we printed 15 tables in a single table by using nested loops.

That’s all for today hope you like the tutorial if you have any queries regarding this article so feel free to contact us below. Thanks for reading.

Leave a Comment