How can you output the results of a for loop in a table with 4 columns in PHP?

To output the results of a for loop in a table with 4 columns in PHP, you can create a table structure and use the modulus operator (%) to determine when to start a new row. Within the loop, you can echo out each element within a table cell (<td>) and increment a counter to keep track of the number of columns. When the counter reaches 4, you can start a new row by outputting a closing and opening table row tags (<tr>).

&lt;?php
$numbers = range(1, 20);

echo &#039;&lt;table&gt;&#039;;
$counter = 0;

foreach ($numbers as $number) {
    if ($counter % 4 == 0) {
        echo &#039;&lt;tr&gt;&#039;;
    }
    
    echo &#039;&lt;td&gt;&#039; . $number . &#039;&lt;/td&gt;&#039;;
    
    $counter++;
    
    if ($counter % 4 == 0) {
        echo &#039;&lt;/tr&gt;&#039;;
    }
}

echo &#039;&lt;/table&gt;&#039;;
?&gt;