What are best practices for automatically incrementing a counter in PHP when adding new elements to an array for HTML output?

When adding new elements to an array for HTML output, it is common to need a counter to keep track of the index of each element. One way to automatically increment this counter in PHP is to use a foreach loop to iterate over the array and increment a separate counter variable within the loop.

<?php
// Sample array of elements for HTML output
$elements = ['Element 1', 'Element 2', 'Element 3'];

// Initialize a counter variable
$counter = 1;

// Iterate over the array and output each element with its corresponding index
foreach ($elements as $element) {
    echo '<p>Element ' . $counter . ': ' . $element . '</p>';
    $counter++;
}
?>