What is the purpose of using a counter variable in PHP when generating HTML elements dynamically?

When generating HTML elements dynamically in PHP, using a counter variable can help keep track of the number of elements being created. This is particularly useful when iterating over an array to generate multiple elements, such as a list of items. The counter variable can be used to assign unique IDs or classes to each element, ensuring they are distinct and can be styled or targeted individually.

<?php
$items = ['Item 1', 'Item 2', 'Item 3'];
$count = 1;

foreach ($items as $item) {
    echo '<div id="item_' . $count . '">' . $item . '</div>';
    $count++;
}
?>