What are common pitfalls when trying to number comments in a PHP news script?

Common pitfalls when trying to number comments in a PHP news script include not resetting the comment count for each news article, not properly incrementing the count for each comment, and not displaying the comment number correctly in the output. To solve this issue, you can create a variable to keep track of the comment count for each news article and reset it for every new article. Increment the count for each comment and display it alongside the comment in the output.

$commentCount = 1; // Initialize comment count for each news article

// Loop through comments for each news article
foreach($comments as $comment) {
    // Display comment number
    echo "Comment " . $commentCount . ": " . $comment['text'] . "<br>";
    
    $commentCount++; // Increment comment count
}

$commentCount = 1; // Reset comment count for the next news article