How can a counter be used to display data in two columns in PHP?

To display data in two columns using a counter in PHP, you can iterate through an array of data and use a counter to determine when to start a new column. By incrementing the counter with each iteration, you can control the placement of data in each column.

$data = array("Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6");

$counter = 0;
echo "<div style='float: left; width: 50%;'>";
foreach($data as $item) {
    if($counter == ceil(count($data) / 2)) {
        echo "</div><div style='float: left; width: 50%;'>";
    }
    echo $item . "<br>";
    $counter++;
}
echo "</div>";