PHP Basics

Easily Create A Table In PHP

Here is a small PHP snippet to easily create a table in PHP.

First create the draw_table function.

$rows = 10; // amout of tr 
$cols = 10;// amjount of td 
function drawTable($rows, $cols){
echo "<table border='1'>"; 

for($tr=1;$tr<=$rows;$tr++){ 
     
    echo "<tr>"; 
        for($td=1;$td<=$cols;$td++){ 
               echo "<td align='center'>".$tr*$td."</td>"; 
        } 
    echo "</tr>"; 
} 

echo "</table>";
}

To draw the table on your page use the function we just created.

drawTable(30, 10);

This will create a table with 30 rows and 10 columns.

Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial