What are the potential pitfalls of incrementing a page count variable within a PHP loop for pagination purposes?

Potential pitfalls of incrementing a page count variable within a PHP loop for pagination purposes include inaccurate page counts if the loop does not iterate over all items, as well as performance issues if the loop is large. To solve this, it's better to calculate the total number of pages based on the total number of items and items per page outside of the loop.

$totalItems = 1000; // total number of items
$itemsPerPage = 10; // items to display per page

$totalPages = ceil($totalItems / $itemsPerPage); // calculate total pages

// loop through items
for ($i = 0; $i < $totalItems; $i++) {
    // display items
}

// pagination logic using $totalPages