In what scenarios would it be beneficial to switch from counting div tags to counting other HTML elements in a PHP application?

When counting div tags in a PHP application, it may be beneficial to switch to counting other HTML elements if the application needs to target specific elements for manipulation or styling. For example, counting heading tags (h1, h2, etc.) can help in generating a table of contents dynamically. Counting anchor tags (a) can help in creating a list of links within a page. By switching to count other HTML elements, developers can have more flexibility in how they interact with the content on the page.

// Counting heading tags (h1, h2, etc.)
$html = '<h1>Title</h1><h2>Subtitle</h2><p>Paragraph</p>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$headings = $dom->getElementsByTagName('h1');
$headingCount = $headings->length;

echo "Number of heading tags: " . $headingCount;